From: Julian Leviston Date: 2011-08-10T09:25:39+09:00 Subject: Re: Problem in typical code --Apple-Mail-C2B03E4E-6B74-411A-ABD7-66B7CC9AD3ED Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii I think the best way to explain it is this: In ruby, text you type is converted into representations of tokens represent= ing objects and methods. The simplest way to think about it at first is to construct a model in your m= ind where there are a lot of objects floating around.=20 When you type this into ruby, you're instructing it to execute the method na= med "to_s" that is stored in the Fixnum class object, and to execute it on t= he Fixnum instance object 45: 45.to_s What that does is create a new string object representing that Fixnum and re= turn it to you. (ie it "falls out the end") This is called method calling, or messaging. You're sending the object 45 th= e message "to_s" and it returns the result of "45". Now, because everything is an object in the way we think about ruby, and eva= de ruby is quite flexible, you can do some clever things. You can obviously create your own class objects and then create your own ins= tance objects of these classes, but the really clever thing we're seeing her= e is that you can re-define the methods inside a class object once it has al= ready been defined. You can add, or modify methods "on the fly" so to speak.= =20 This is what's happening with your code. You're ripping open the Fixnum clas= s object - the one that ruby uses to represents fixed numbers... That is, in= tegers that are fairly small... And you're adding some methods. All the exis= ting functionality will remain unless you choose a method name that is alrea= dy in use and that will obviously replace an existing method if you do that.= =20 It's important to think in your kind about context. When you're "inside" a c= lass definition such as this: class Fixnum def seconds self end end Inside the class...end context, self means "the object we're currently refer= ring 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 wil= l 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 l= ot 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/=20 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 : >=20 > 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 >=20 >=20 >=20 >=20 > AGAIN , EXCUSE ME!!! >=20 > --=20 > Posted via http://www.ruby-forum.com/. >=20 --Apple-Mail-C2B03E4E-6B74-411A-ABD7-66B7CC9AD3ED--