# Problem in typical code

**URL:** <https://www.ruby-forum.com/t/problem-in-typical-code/210494>\
**Category:** Ruby\
**Created:** [August 9, 2011, 7:25pm UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494 "2011-08-09T19:25:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 9, 2011, 7:25pm UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/1 "2011-08-09T19:25:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 9, 2011, 7:32pm UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/2 "2011-08-09T19:32:33Z")

</div>

On Tue, Aug 9, 2011 at 7:25 PM, amir e. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

–  
Phillip G.

[phgaw.posterous.com](http://phgaw.posterous.com) | [twitter.com/phgaw](http://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

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 9, 2011, 7:39pm UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/3 "2011-08-09T19:39:21Z")

</div>

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

> On Tue, Aug 9, 2011 at 7:25 PM, amir e. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

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

just kidding

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 9, 2011, 8:38pm UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/4 "2011-08-09T20:38:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 9, 2011, 9:03pm UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/5 "2011-08-09T21:03:31Z")

</div>

On Tue, Aug 9, 2011 at 8:38 PM, amir e. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> EXCUSE ME !!!  
> THIS IS THE CODE :

No need to use caps. 🙂

> 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](http://phgaw.posterous.com) | [twitter.com/phgaw](http://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

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 10, 2011, 1:05am UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/6 "2011-08-10T01:05:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 10, 2011, 2:26am UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/7 "2011-08-10T02:26:11Z")

</div>

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/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
real fastcgi rails deploy process! Check it out now!

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 10, 2011, 2:55am UTC](https://www.ruby-forum.com/t/problem-in-typical-code/210494/8 "2011-08-10T02:55:11Z")

</div>

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/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
real fastcgi rails deploy process! Check it out now!
