From: Victor Shepelev Date: 2006-04-14T23:57:30+09:00 Subject: Re: Default value for any type? > > Hello all. > > > > I have code: > > > > def create_default(type) > > type.new > > end > > > > But with, for example, Float, it suddenly said: > > > > create_default(Float) #<=== undefined method `new' for Float:Class > > (NoMethodError) > > > > Hmmm? How can I uniformly obtain default value for given class? > > What should the default value for a Float be? What are you trying to > really do here? > > There are a number of classes that you're not going to be able to call > #new on. Not so long time ago I've been a C++ guy. There we could do: float(); //0.0 int(); //0 ...etc... Generalized version: template T create_default() { return T(); } //usage: create_default(); // => 0.0 create_default(); // => "" and so on. I think, argument-less constructor, creating some "default" value, is a very useful thing. (yes, I can define Float#default and so on for myself ;) But I want to know, *why*?) > -austin > -- > Austin Ziegler * halostatue@gmail.com > * Alternate: austin@halostatue.ca Victor.