Rails 2.2.2 to_date and to_datetime methods

James Byrne 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/index.html?a=M001206&name=to_date

Robert Walker 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/index.html?a=M001206&name=to_date

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.