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

P

paul

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

ara.t.howard

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

it can't be done in ruby. there are many thing which can't be done in java
which can be done in ruby and vise versa - better to learn the idioms and use
them.

search the arvhices for more - it's come up many, many times.

-a
 
P

Patrick Hurley

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
 
J

James Edward Gray II

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
 
P

paul

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
 
T

Timothy Hunter

paul said:
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.
 
J

Jan Svitok

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) ;-)
 
M

Marcin Mielżyński

paul said:
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
 
R

Robert Klemme

paul said:
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
 
T

Tom Werner

paul said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top