[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Overwriting the Integer class for method succ! (instead of just succ

paul

11/9/2006 7:22:00 PM

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?...

10 Answers

Ara.T.Howard

11/9/2006 7:35:00 PM

0

Patrick Hurley

11/9/2006 7:43:00 PM

0

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

Tim Hunter

11/9/2006 7:43:00 PM

0


paul 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...

http://wiki.rubygarden.org/Ruby/page/show/Increme...

James Gray

11/9/2006 8:10:00 PM

0

On Nov 9, 2006, at 1:25 PM, paul wrote:

> A little thing that is bugging me is that I don't know how to do a
> "count++" (like in Java) in Ruby.

One more character:

count+=1

James Edward Gray II


paul

11/9/2006 9:15:00 PM

0

Thanks all! I became so much wiser now. I gues I didn't find this
information googling, because I assumed that it would be possible,
therefore searching "ruby integer overwrite value" and such...

Now I still wonder, the only source-code I found for the Integer class
was in C. From this I wasn't able to figure out how I would go IF i'd
like to write this count++ (and with that modifying all 1's to 2's
e.g.).

This kind of information, is not in the Programming Ruby Built-in
Classes and Methods chapter. Is this because that only contains the
public methods, and not the protected's? Or are there no such thing as
protected's for Integer (because it's in C?)?

Cheers again

Tim Hunter

11/9/2006 10:31:00 PM

0

paul wrote:
> Thanks all! I became so much wiser now. I gues I didn't find this
> information googling, because I assumed that it would be possible,
> therefore searching "ruby integer overwrite value" and such...
>
> Now I still wonder, the only source-code I found for the Integer class
> was in C. From this I wasn't able to figure out how I would go IF i'd
> like to write this count++ (and with that modifying all 1's to 2's
> e.g.).
>
> This kind of information, is not in the Programming Ruby Built-in
> Classes and Methods chapter. Is this because that only contains the
> public methods, and not the protected's? Or are there no such thing as
> protected's for Integer (because it's in C?)?
>
> Cheers again
>
>
>
I think it's because this behavior is considered to be part of Ruby's
fundamental design. Changing this behavior requires deep knowledge of
Ruby internals (and C programming) and is outside the scope of a book
like Programming Ruby.

Jano Svitok

11/9/2006 10:39:00 PM

0

On 11/9/06, paul <pjvleeuwen@gmail.com> wrote:
> Thanks all! I became so much wiser now. I gues I didn't find this
> information googling, because I assumed that it would be possible,
> therefore searching "ruby integer overwrite value" and such...
>
> Now I still wonder, the only source-code I found for the Integer class
> was in C. From this I wasn't able to figure out how I would go IF i'd
> like to write this count++ (and with that modifying all 1's to 2's
> e.g.).
>
> This kind of information, is not in the Programming Ruby Built-in
> Classes and Methods chapter. Is this because that only contains the
> public methods, and not the protected's? Or are there no such thing as
> protected's for Integer (because it's in C?)?

If you know a bit of C, it's pretty interesting to read the ruby
sources from time to time. They're very readble, and it's the
definitive documentation after all ;-)
The code for let say Array or Integer etc. is pretty easy (maybe some
details are tough). You'll get the picture how the things are working
on the inner side.

There's even guide how to read them (in what order) ;-)

Marcin Mielzynski

11/9/2006 11:25:00 PM

0

paul wrote:

> Now I still wonder, the only source-code I found for the Integer class
> was in C. From this I wasn't able to figure out how I would go IF i'd
> like to write this count++ (and with that modifying all 1's to 2's
> e.g.).
>
> This kind of information, is not in the Programming Ruby Built-in
> Classes and Methods chapter. Is this because that only contains the
> public methods, and not the protected's? Or are there no such thing as
> protected's for Integer (because it's in C?)?
>
> Cheers again
>

Nope, since Fixnum is an immediate value (variables are copied - not the
values they refer to) and there is only one instance of a specific
number (so only one 1, 2, 3 etc...). Changing it would affect all other
instances in the whole interpreter (in fact a single Fixnum instance
takes only 4/8 bytes depending on the architecture - so they cant have a
singleton).

But it is possible to tweak Float instances ;) , although they are (seem
to be) immutable, but not immediate (only on the c side - but quite easy).

lopex

Robert Klemme

11/10/2006 6:43:00 PM

0

paul wrote:
> Thanks all! I became so much wiser now. I gues I didn't find this
> information googling, because I assumed that it would be possible,
> therefore searching "ruby integer overwrite value" and such...
>
> Now I still wonder, the only source-code I found for the Integer class
> was in C. From this I wasn't able to figure out how I would go IF i'd
> like to write this count++ (and with that modifying all 1's to 2's
> e.g.).

You cannot do this (even in C) because Fixnum is immutable. Every
Fixnum instance has a fixed value and that never changes (like Integer
instances in Java).

Kind regards

robert

Tom Werner

11/10/2006 7:29:00 PM

0

paul 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.

The rubyish way to do

count++

is

count += 1

Many Ruby first-timers are disconcerted by the lack of the ++ operator,
as I was myself. After some reflection and experience with the language,
I no longer miss it. It's one extra keystroke (or three if you pad with
spaces like I do), but it forever eliminates the confusion of ++var vs.
var++. Once you accept that there's no ++ syntactic sugar in Ruby and
embrace += and -=, I think you'll be a much happier coder.

Tom