Problem in typical code

Hi
In a ruby learning book I faced to below code, I don’t understand what
does second method do ? and what is use of self in second method ?
please help me .
thanks

On Tue, Aug 9, 2011 at 7:25 PM, amir e. [email protected] wrote:

Hi
In a ruby learning book I faced to below code, I don’t understand what
does second method do ? and what is use of self in second method ?
please help me .
thanks

Posting the code would help a lot. :slight_smile:


Phillip G.

phgaw.posterous.com | twitter.com/phgaw | gplus.to/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

On 08/09/2011 07:30 PM, Phillip G. wrote:

On Tue, Aug 9, 2011 at 7:25 PM, amir e. [email protected] wrote:

Hi
In a ruby learning book I faced to below code, I don’t understand what
does second method do ? and what is use of self in second method ?
please help me .
thanks
Posting the code would help a lot. :slight_smile:

I was about to say the same, for luck i’m not the only (insert country
of choice) zealot around here LOL

just kidding

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!!!

On Tue, Aug 9, 2011 at 8:38 PM, amir e. [email protected] wrote:

EXCUSE ME !!!
THIS IS THE CODE :

No need to use caps. :slight_smile:

def days
self * 60 * 60 * 24
end
end

Explanation of the code:

If you type this into irb, and write something like “2.days”, the
Fixnum 2 (Fixnums are a bit of a special case in Ruby, since they
don’t usually get created, but just are) gets multiplied by seconds,
minutes, then hours. This happens because “self” is a keyword in Ruby,
which means “I want to do this to myself”.

A made up example:

class Car
def turn_key
self.motor = “on”
end

def motor=(state) # A manually created ‘setter method’
@motor = state # “@motor” is an ‘instance variable’, meaning it is
valid only within a single Car object.
end
end


Phillip G.

phgaw.posterous.com | twitter.com/phgaw | gplus.to/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

hi Amir,

“self” can be a bit confusing at times, but you can think of it as
representing whatever class (or instance of that class) you are dealing
with…

for example:

here, “self” is whatever Fixnum you happen to use with the #double
method

class Fixnum

  def double
    p self * 2
  end

end

3.double

=> 6

here, “self” is whatever Array you happen to create and use with the
#double method…

class Array

  def double
    self.each_with_index{|entry, index|
      new = entry * 2
      self[index] = new
    }
  end

end

 foo = ["a", "b", "c"]
 foo.double
 p foo

 => ["aa", "bb", "cc"]
  • j

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” :wink:

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!

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!