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.methodsNoMethodError: 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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_dateThanks. 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Byrne <rails-mailing-list@andreas- s.net> 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? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---