On Wed, Mar 6, 2013 at 2:00 PM, Pritam D. [email protected] wrote:
From the doc: Class: Fixnum (Ruby 1.9.3)
I found the below features :
(a) Fixnum objects have immediate value. This means that when they are
assigned or passed as parameters, the actual object is passed, rather
than a reference to that object. - Can the same be shown in IRB. Hope
then only it will be correctly understood by me.
The fact that Fixnums are “immediate values” is just an implementation
detail. From the perspective of a user of the language it does not
make a functional difference (this is about performance only). I
suggest you take a different route and start with basic understanding
of how numeric classes and general OO and variables work in Ruby.
(b) Assignment does not alias Fixnum objects. - What does it actually
From a logical point of view it does:
irb(main):001:0> a = 1
irb(main):003:0> b = a
irb(main):005:0> a == b
irb(main):006:0> a.equal? b
irb(main):007:0> a.object_id == b.object_id
(c) “There is effectively only one Fixnum object instance for any given
integer value, so, for example, you cannot add a singleton method to a
Fixnum.” - Couldn’t understand the reason of not to add the
singleton method with Fixnum object instances.
I think the reasoning is flawed: the fact that there is only one
instance of every Fixnum does by no means prevent associating
singleton methods with the instance. For example, it’s still possible
to store instance variables in a Fixnum instance:
same a and b as above
irb(main):009:0> a.instance_variable_set ‘@x’, “yeah!”
irb(main):011:0> a.instance_variable_get ‘@x’
irb(main):013:0> b.instance_variable_get ‘@x’
irb(main):014:0> 1.instance_variable_get ‘@x’
irb(main):015:0> 2.instance_variable_get ‘@x’
It’s just an implementation decision to not allow singleton methods for
– the above (b) and (c) points are also made me confused.
As I said above. For understanding when Ruby will switch between
types (your original question, as I understand it) this is irrelevant.
If you want to understand how that works (method #coerce is central
to that) you can read my blog post which describes how to create a
class which adheres to the conventions of operator overloading and
doing math in Ruby: