Patrick Hurley
11/9/2006 7:43:00 PM
On 11/9/06, paul <pjvleeuwen@gmail.com> wrote:
> Hi all,
>
> A little thing that is bugging me is that I don't know how to do a
> "count++" (like in Java) in Ruby. I found that Ruby has a method succ /
> next, but that still leaves me doing "count = count.succ" A simple
> "count.succ!" doesn't seem to exist...
>
> Now I try to overwrite the class, but I don't know the internal name of
> the value stored in the integer class. "self = self + 1" is not valid,
> so I tried:
> class Integer
> def succ!
> self.value = succ
> end
> end
> , but value is not a valid name. When browsing the internet I found
> some C code (is *all* of Ruby written in C?). In this C code I found
> some names like 'int_int_p' and 'VALUE', but those don't seem right
> either...
>
> Anyone?...
>
>
>
The short answer is what you are trying is impossible. Ruby variables
are just names to actual objects. Methods that modify self, are really
modifying the "actual object":
a = "pat" => "pat"
b = a => "pat"
a.object_id => 24077070
b.object_id => 24077070
b.upcase! => "PAT"
a => "PAT"
a.object_id => 24077070
b.object_id => 24077070
Note that the object_id never changes. Now do this:
1.object_id => 3
a = 1 => 1
a.object_id => 3
If you could write succ! for a Fixnum, 1.succ! would globally change
every one in your application to 2 :-)
I have been a C/C++ programmer for almost 20 years (wow I am getting
old), and when I first used Ruby I missed my ++/--, but I got over it
-- just enjoy Ruby for what it is.
pth