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

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

···

On Fri, 10 Nov 2006, 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...

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

--
my religion is very simple. my religion is kindness. -- the dalai lama

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 :slight_smile:

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

···

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

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/IncrementOperator

One more character:

count+=1

James Edward Gray II

···

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.

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

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

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.

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

···

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

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 :wink: , although they are (seem to be) immutable, but not immediate (only on the c side - but quite easy).

lopex

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