On Wed, Mar 6, 2013 at 2:00 PM, Pritam Dey <[email protected]> wrote:
> From the doc: http://ruby-doc.org/core-1.9.3/Fixnum.html
>
> I found the below features :
>
> Fixnum:
>
> (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
> then?

>From a logical point of view it does:

irb(main):001:0> a = 1
=> 1
irb(main):002:0> a.class
=> Fixnum
irb(main):003:0> b = a
=> 1
irb(main):004:0> b.class
=> Fixnum
irb(main):005:0> a == b
=> true
irb(main):006:0> a.equal? b
=> true
irb(main):007:0> a.object_id == b.object_id
=> true

> (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!"
=> "yeah!"
irb(main):010:0> a.instance_variables
=> [:@x]
irb(main):011:0> a.instance_variable_get '@x'
=> "yeah!"
irb(main):012:0> b.instance_variables
=> [:@x]
irb(main):013:0> b.instance_variable_get '@x'
=> "yeah!"
irb(main):014:0> 1.instance_variable_get '@x'
=> "yeah!"
irb(main):015:0> 2.instance_variable_get '@x'
=> nil

It's just an implementation decision to not allow singleton methods for Fixnum.

> -- 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:
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to