From: Matthew Desmarais Date: 2006-04-15T00:57:42+09:00 Subject: Re: Default value for any type? Victor Shepelev wrote: >>> 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. > Hi Victor, I think that Austin's question about what you are trying to accomplish is probably a good. one. If you can post some Ruby code that would rely on something like this that would be great. Maybe the folks on the list can help you find a solution for you. Regards, Matthew Desmarais