Re: Ruby doesn't implement x++ for Fixnum's because ???



On Fri, Oct 30, 2009 at 1:51 PM, lith <minilith@xxxxxxxxx> wrote:
IMHO the story goes like this: absence of postfix ++ and -- is a
consequence of the fact that numeric types are immutable in Ruby which
makes an assignment necessary for these operators.

You could "decorate" it with such a method though. This could look
something like this:

   class FixNum < BasicObject

       def initialize(fn)
           @fn = fn
       end

       def succ!
           @fn += 1
       end

       def method_missing(*args, &block)
           @fn.send(*args, &block)
       end

Hi lith,

can you extol your knowledge on me about what method_missing is doing?
I get what *args and &block is, but not quite sure what's going on?

   end

   a = FixNum.new(1)
   a.succ!
   p a
   => 2

Whether this is good practice or marginally useful ...





--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives.



Relevant Pages

  • Re: Ruby doesnt implement x++ for Fixnums because ???
    ... consequence of the fact that numeric types are immutable in Ruby which ... You could "decorate" it with such a method though. ... def initialize ...
    (comp.lang.ruby)
  • Re: "10 Minutes to Your First Ruby Application" Deconstructed!
    ... I am a newbie to Ruby. ... def initialize(app_map) ...
    (comp.lang.ruby)
  • Re: Creating variables on an OpenStruct with dynamic names
    ... having previously done a little ruby. ...    base.extend ClassMethods ... still use that to create an Array of all rows. ... # now create the row object ...
    (comp.lang.ruby)
  • Re: Access to method in superclass
    ...   public Airplane{ ... worked hard to convert the Java program directly into a Ruby program. ... emphasis in programming is Agile Programming and Test-Driven or ...
    (comp.lang.ruby)
  • Re: Hello
    ... is there a disk that can be sent out for us to use for ruby and ruby on rails. ...   puts ... I did this on Macintosh OS X (the output is cut&pasted from my Terminal window), but you will get similar results on any system. ... The irb program, provided as a part of the core Ruby package, provides a great way of experimenting with programs. ...
    (comp.lang.ruby)