tags:

views:

91

answers:

5

Is everything in ruby an object? Does this include Fixnums?

A: 

Yes everything is an object in ruby, and that includes Fixnum

Alan
A: 

Yup.

> Fixnum.is_a?(Object)   #=> true

To see the chain of inheritance:

> pp Fixnum.ancestors
[Fixnum,
 Integer,
 Precision,
 Numeric,
 Comparable,
 Object,
  ...
 Kernel]
 => nil 
Dex
`#ancestors` gives all the included modules as well, not just the inheritance chain.
jtbandes
Also, `pp` isn't available by default, at least in 1.8.7p174.
jtbandes
Yes, I know! thx.
Dex
+2  A: 

Yes. Fixnum is a class, which inherits from Integer, which inherits from Numeric, which finally inherits from Object.

Or, why don't you just ask it? :)

1.is_a? Object # => true
1.class # => Fixnum
Fixnum.is_a? Object # => true

Reading the Ruby info and documentation on the website is a good idea too.

jtbandes
+11  A: 

Depends on what you mean by "everything". Fixnums are, as the others have demonstrated. Classes also are, as instances of class Class. Methods, operators and blocks aren't, but can be wrapped by objects (Proc). Simple assignment is not, and can't. Statements like while also aren't and can't. Comments obviously also fall in the latter group.

Most things that actually matter, i.e. that you would wish to manipulate, are objects (or can be wrapped in objects).

Amadan
+1 Love these counter-examples. Saying that everything is an object without considering EVERYTHING is slightly brainwashed :)
Jakob
+1 - Superb that's what i was trying to say in some other question, from which this question has been spawned.
Webbisshh
A: 

Ruby doen't have any primitives (like int, char etc in java), so every value (anything that can sit on the right of an assignment statement) is an object. However, control statements, methods, and other features of the language syntax aren't.

mistertim