From: dblack@... Date: 2006-04-15T00:50:32+09:00 Subject: Re: Default value for any type? Hi -- On Fri, 14 Apr 2006, 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*?) I guess that's what I want to know too: why would you expect Ruby classes to have default instances, and why would you need them too? David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" coming in PDF April 15, and in paper May 5! http://www.manning.com/black