From: Julian Leviston Date: 2011-08-10T09:49:44+09:00 Subject: Re: Problem in typical code I forgot to say once you have this class redefined you can then write interesting things like this: 130.seconds / 1.minutes And ruby will tell you 2. Clever, no? Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now! On 10/08/2011, at 10:25 AM, Julian Leviston wrote: > I think the best way to explain it is this: > > In ruby, text you type is converted into representations of tokens representing objects and methods. > > The simplest way to think about it at first is to construct a model in your mind where there are a lot of objects floating around. > > When you type this into ruby, you're instructing it to execute the method named "to_s" that is stored in the Fixnum class object, and to execute it on the Fixnum instance object 45: > > 45.to_s > > What that does is create a new string object representing that Fixnum and return it to you. (ie it "falls out the end") > > This is called method calling, or messaging. You're sending the object 45 the message "to_s" and it returns the result of "45". > > Now, because everything is an object in the way we think about ruby, and evade ruby is quite flexible, you can do some clever things. > > You can obviously create your own class objects and then create your own instance objects of these classes, but the really clever thing we're seeing here is that you can re-define the methods inside a class object once it has already been defined. You can add, or modify methods "on the fly" so to speak. > > This is what's happening with your code. You're ripping open the Fixnum class object - the one that ruby uses to represents fixed numbers... That is, integers that are fairly small... And you're adding some methods. All the existing functionality will remain unless you choose a method name that is already in use and that will obviously replace an existing method if you do that. > > It's important to think in your kind about context. When you're "inside" a class definition such as this: > > class Fixnum > def seconds > self > end > end > > Inside the class...end context, self means "the object we're currently referring to". It's whichever object is the target of the method. > > So here, in the case of our Fixnum 45, because seconds is an instance object > method, it means the number 45 itself. So sending 45 the message seconds will return 45 itself. > > If I were writing that code, I would probably have written it like this: > > class Fixnum > def seconds > self > end > def minutes > self * 60.seconds > end > def hours > self * 60.minutes > end > def days > self * 24.hours > end > end > > Experimentation is a very powerful tool in learning. Use IRB a lot and do a lot of micro experiments, or "tests" ;) > > Julian > > Blog: http://random8.zenunit.com/ > Twitter: http://twitter.com/random8r > Learn: http://sensei.zenunit.com/ > New video up now at http://sensei.zenunit.com/ > real fastcgi rails deploy process! Check it out now! > > > On 10/08/2011, at 4:38 AM, "amir e." wrote: > >> EXCUSE ME !!!!!!!!!! >> THIS IS THE CODE : >> >> class Fixnum >> def seconds >> self >> end >> def minutes >> self * 60 >> end >> def hours >> self * 60 * 60 >> end >> def days >> self * 60 * 60 * 24 >> end >> end >> >> >> >> >> AGAIN , EXCUSE ME!!! >> >> -- >> Posted via http://www.ruby-forum.com/. >>