Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Overwriting the Integer class for method succ! (instead of just succ)

Reply
Thread Tools

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

 
 
paul
Guest
Posts: n/a
 
      11-09-2006
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?...

 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      11-09-2006
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?...


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
--
my religion is very simple. my religion is kindness. -- the dalai lama

 
Reply With Quote
 
 
 
 
Patrick Hurley
Guest
Posts: n/a
 
      11-09-2006
On 11/9/06, paul <(E-Mail Removed)> 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

 
Reply With Quote
 
rmagick@gmail.com
Guest
Posts: n/a
 
      11-09-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...


http://wiki.rubygarden.org/Ruby/page...rementOperator

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      11-09-2006
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


 
Reply With Quote
 
paul
Guest
Posts: n/a
 
      11-09-2006
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

 
Reply With Quote
 
Timothy Hunter
Guest
Posts: n/a
 
      11-09-2006
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.

 
Reply With Quote
 
Jan Svitok
Guest
Posts: n/a
 
      11-09-2006
On 11/9/06, paul <(E-Mail Removed)> 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)

 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      11-09-2006
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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-10-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overwriting an entire class from a disk record Dom Bannon C++ 10 10-07-2010 09:23 AM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Self-overwriting class - is 'volatile' needed for field keepers? Ole Nielsby C++ 3 10-11-2006 01:20 AM
overwriting method in baseclass Harald Massa Python 5 02-09-2005 09:13 PM
overwriting fields from abstract class Peter Grison Java 3 04-27-2004 05:14 PM



Advertisments