From: Matthew Kerwin Date: 2013-04-15T13:06:52+09:00 Subject: Re: Wondering why no "increment" or "decrement" operator in ruby tamouse mailing lists wrote in post #1105640: > On Sun, Apr 14, 2013 at 10:23 PM, Matthew Kerwin > wrote: >> Incidentally, if you're using MRI, because of a clever optimisation your >> 'a' variable literally holds the value `1`, not a reference per se. > > I guess I don't understand this last part; I can still call an > instance method on a, so it must be more than just a value..., no? As > I can call an instance method on 1. I guess I don't quite get what you > mean by 'value'... In the C code underlying MRI, ruby objects are referred to by integers called 'VALUE's, which are effectively references to the instantiated objects. However for fixnums (and the new flonums) the VALUE is actually a reversibly-mangled version of the actual value. Here's a transcript from IRB on my 64 bit Linux box in a recent build of trunk: irb:001> 1.object_id >> 1 => 1 irb:002> 328.object_id >> 1 => 328 irb:003> -2.object_id >> 1 => -2 Because the numeric value is known, there's no need to instantiate a "proper" object to encapsulate it; rather the interpreter can look at the VALUE, see that it has the "is a Fixnum" bits set, and dispatch the appropriate methods through the Fixnum class. On that point: Fixnum "objects" don't even have a singleton: irb:004> class << 1; end TypeError: can't define singleton from (irb):4 from /usr/local/bin/irb21:12:in `
' irb:005> a = 1; def a.foo; end TypeError: can't define singleton from (irb):5 from /usr/local/bin/irb21:12:in `
' Every method you send to a Fixnum object goes through the class. -- Posted via http://www.ruby-forum.com/.