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

Discussion in 'Ruby' started by paul, Nov 9, 2006.

  1. paul

    paul Guest

    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, Nov 9, 2006
    #1
    1. Advertising

  2. paul

    Guest

    Re: Overwriting the Integer class for method succ! (instead of justsucc)

    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
    , Nov 9, 2006
    #2
    1. Advertising

  3. On 11/9/06, 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?...
    >
    >
    >


    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
    Patrick Hurley, Nov 9, 2006
    #3
  4. paul

    Guest

    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
    , Nov 9, 2006
    #4
  5. 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
    James Edward Gray II, Nov 9, 2006
    #5
  6. paul

    paul Guest

    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, Nov 9, 2006
    #6
  7. Re: Overwriting the Integer class for method succ! (instead of justsucc)

    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.
    Timothy Hunter, Nov 9, 2006
    #7
  8. paul

    Jan Svitok Guest

    On 11/9/06, 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?)?


    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) ;-)
    Jan Svitok, Nov 9, 2006
    #8
  9. Re: Overwriting the Integer class for method succ! (instead of justsucc)

    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
    Marcin Mielżyński, Nov 9, 2006
    #9
  10. Re: Overwriting the Integer class for method succ! (instead of justsucc)

    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
    Robert Klemme, Nov 10, 2006
    #10
  11. paul

    Tom Werner Guest

    Re: Overwriting the Integer class for method succ! (instead of justsucc)

    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
    Tom Werner, Nov 10, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harald Massa

    overwriting method in baseclass

    Harald Massa, Feb 7, 2005, in forum: Python
    Replies:
    5
    Views:
    323
    Harald Massa
    Feb 9, 2005
  2. Neha
    Replies:
    37
    Views:
    1,392
  3. Hal Fulton

    Time#succ ?

    Hal Fulton, Nov 23, 2003, in forum: Ruby
    Replies:
    7
    Views:
    129
    Niklas Frykholm
    Nov 28, 2003
  4. illocutionist
    Replies:
    3
    Views:
    134
    Mauricio Fernández
    Mar 6, 2004
  5. Jeff Mitchell

    Propose Range.new(..., &succ)

    Jeff Mitchell, Apr 24, 2004, in forum: Ruby
    Replies:
    2
    Views:
    100
    Tim Sutherland
    Apr 26, 2004
Loading...

Share This Page