Forum: Ruby on Rails Rails 2.2.2 to_date and to_datetime methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James B. (Guest)
on 2009-02-04 22:27
In the console I see this behaviour:


>> "19270412000000".to_date.methods
=> ["ns?", "mon", "ago", "end_of_month", "months_since",
"default_inspect", "minus_without_duration", ...
>> "19270412000000".to_date.class
=> Date
>> "19270412000000".class
=> String
>> String.to_date.methods
NoMethodError: undefined method `to_date' for String:Class
        from (irb):23

Can someone explain how a method may be present in an object instance
but not in the object class?  Does Rails insert singleton methods into
these instances?
Robert W. (Guest)
on 2009-02-04 22:58
James B. wrote:
> In the console I see this behaviour:
>
>
>>> "19270412000000".to_date.methods
> => ["ns?", "mon", "ago", "end_of_month", "months_since",
> "default_inspect", "minus_without_duration", ...
>>> "19270412000000".to_date.class
> => Date
>>> "19270412000000".class
> => String
>>> String.to_date.methods
> NoMethodError: undefined method `to_date' for String:Class
>         from (irb):23
>
> Can someone explain how a method may be present in an object instance
> but not in the object class?  Does Rails insert singleton methods into
> these instances?
String has no class method "to_date" yet it does have a "to_date"
instance method. However, Ruby itself has neither a class nor instance
method named "to_date" for it's String class.

The magic happens here:
http://www.railsbrain.com/api/rails-2.2.2/doc/inde...
Robert W. (Guest)
on 2009-02-04 23:05
By the way it is possible to add methods to instances of objects in
Ruby. Although that is not what's being done in your case. Rails is
simply extending the Ruby String class to add the method to all
instances of String.

Here is a nice blog about singleton methods in case you want to know
more:
http://pmade.com/articles/2008/ruby-singleton
James B. (Guest)
on 2009-02-04 23:34
Robert W. wrote:

> String has no class method "to_date" yet it does have a "to_date"
> instance method. However, Ruby itself has neither a class nor instance
> method named "to_date" for it's String class.
>
> The magic happens here:
> http://www.railsbrain.com/api/rails-2.2.2/doc/inde...

Thanks.  If I follow this correctly then what happens is that the String
Class is opened inside the Rails libraries and the instance methods
to_date etc. are slipped in.
Julian L. (Guest)
on 2009-02-05 02:16
(Received via mailing list)
Basic oop. Classes have class methods, instances hve instance methods.
Makes sense if you understand what a class is. Animal is a class. If
you want to create an instance, you say Animal.new. That gives you an
animal object which is not the class. It would have been better if
they'd called it new_instance, coz that's what it does. Now the
instance is a particular one. It's my cat named mrs Peabody. The class
represents the blueprint of what it is to be animal without any
particulars. I can't tell my cat "new" coz she doesn't respond to that
method. Only the class does. In this way, also, classes don't respond
to instance methods. Animal couldn't tell you its hair colour for
instance - it doesn't have hair. It does know that animals have hair
though! Get it? This is why active record model classes respond to the
find command. The model class represents the set of all of that class
of things, hence it's fairly well synonymous with a table of data Whig
is a collection of the PARTICULARs of that class of things. You
couldn't get a particular instance of a model object to find another
object for instance. - unless you particularly wanted that behaviour.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 05/02/2009, at 7:27 AM, James B. <rails-mailing-list@andreas-
This topic is locked and can not be replied to.