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?

Victor.

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.

-austin

···

On 4/14/06, Victor Shepelev <vshepelev@imho.com.ua> 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?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

class Float
  def Float.new
    0.0
  end
end

···

On 4/14/06, Victor Shepelev <vshepelev@imho.com.ua> 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?

> 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<typename T>
T create_default()
{
  return T();
}

//usage:
create_default<float>(); // => 0.0
create_default<std::string>(); // => ""

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 :wink: But I want to know,
*why*?)

-austin
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Victor.

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<typename T>
T create_default()
{
return T();
}

//usage:
create_default<float>(); // => 0.0
create_default<std::string>(); // => ""

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 :wink: 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!

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<typename T>
T create_default()
{
  return T();
}

//usage:
create_default<float>(); // => 0.0
create_default<std::string>(); // => ""

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 :wink: 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

s/too\?/to?/ :slight_smile:

David

···

On Sat, 15 Apr 2006, dblack@wobblini.net wrote:

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 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!

From: Matthew Desmarais [mailto:desmarm@gmail.com]
Sent: Friday, April 14, 2006 6:58 PM
To: ruby-talk ML
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<typename T>
> T create_default()
> {
> return T();
> }
>
> //usage:
> create_default<float>(); // => 0.0
> create_default<std::string>(); // => ""
>
> 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 :wink: 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.

Hmmm... Maybe I beginning to understand your point. The task was: before
serializing (or storing in DB) some complex class, ensure that all fields
aren't nil. I does some metaprogramming, so description of the fields looked
like:

class Something
  data :name, String, :default => ''
  data :price, Float, :default => 0.0
  data :quan, Fixnum, :default => 0
  data :type, String, :default => 'unknown'
  data :coef, Float , :default = > 1.0

  def store_in_db
    self.validate!
    ....
  end
  
  def validate!
    #ensure each field isn't nil and has right type;
    #set it's value to default in other case
  end
end

(I hope the code above is quite self-explaining.)
Later I saw that most of default values seems to like "empty value" of the
corresponding type (I was C++ guy, remember). So, I want not to write
:default => ... for :name, :price, :quan

But after this topic (and Austins and yours answers) I think that overall
construction don't looks very rubyish. Maybe, I must look for alternatives.

Regards,
Matthew Desmarais

Victor

···

-----Original Message-----

This reminds me of the Metakoans quiz, actually:
http://www.rubyquiz.com/quiz67.html

If you haven't seen that, it may give you some ideas.

--Wilson.

···

On 4/14/06, Victor Shepelev <vshepelev@imho.com.ua> wrote:

> -----Original Message-----
> From: Matthew Desmarais [mailto:desmarm@gmail.com]
> Sent: Friday, April 14, 2006 6:58 PM
> To: ruby-talk ML
> 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<typename T>
> > T create_default()
> > {
> > return T();
> > }
> >
> > //usage:
> > create_default<float>(); // => 0.0
> > create_default<std::string>(); // => ""
> >
> > 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 :wink: 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.

Hmmm... Maybe I beginning to understand your point. The task was: before
serializing (or storing in DB) some complex class, ensure that all fields
aren't nil. I does some metaprogramming, so description of the fields looked
like:

class Something
  data :name, String, :default => ''
  data :price, Float, :default => 0.0
  data :quan, Fixnum, :default => 0
  data :type, String, :default => 'unknown'
  data :coef, Float , :default = > 1.0

  def store_in_db
    self.validate!
    ....
  end

  def validate!
    #ensure each field isn't nil and has right type;
    #set it's value to default in other case
  end
end

(I hope the code above is quite self-explaining.)
Later I saw that most of default values seems to like "empty value" of the
corresponding type (I was C++ guy, remember). So, I want not to write
:default => ... for :name, :price, :quan

But after this topic (and Austins and yours answers) I think that overall
construction don't looks very rubyish. Maybe, I must look for alternatives.
>

This reminds me of the Metakoans quiz, actually:
Ruby Quiz - metakoans.rb (#67)

If you haven't seen that, it may give you some ideas.

Ooooooh! Looks really very interesting. Studying.
Big thanks!

--Wilson.

Victor.