19

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

1
  • I don't think methods in ruby can be treated like objects. Apr 18, 2017 at 9:25

6 Answers 6

48

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).

6
  • 1
    +1 Love these counter-examples. Saying that everything is an object without considering EVERYTHING is slightly brainwashed :)
    – Jakob
    Aug 7, 2010 at 9:44
  • +1 - Superb that's what i was trying to say in some other question, from which this question has been spawned.
    – RubyDubee
    Aug 10, 2010 at 7:52
  • Why isn't while an object?
    – Ziggy
    Jun 12, 2014 at 22:46
  • 1
    @Ziggy: I am not sure what you're asking, but... why would it be?
    – Amadan
    Jun 13, 2014 at 0:49
  • 1
    Yeah, I mean to say "I wonder why they didn't try to make while an object that takes a block". I mean, I know that could lead to some janky magic, but at the same time if you set out to write a language where "everything's an object" why not go all the way. Why not let => be a method. Why not find a way to make argument lists an object. Why not! I guess it's because you would end up with a crazy eso-lang, rather than an industry standard.
    – Ziggy
    Jun 16, 2014 at 20:30
6

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.

1
  • Note- From rails 2.4.0 Fixnum and Bignum added into Integer Class. Now- 1.is_a? Object # => true 1.class # => Integer (not Fixnum).
    – S.Yadav
    Oct 26, 2018 at 4:27
5

Practically everything in Ruby is an Object, with the exception of control structures. Whether or not under the covers a method, code block or operator is or isn't an Object, they are represented as Objects and can be thought of as such.

Take a code block for example:

def what_is(&block)
  puts block.class
  puts block.is_a? Object
end

> what_is {}
Proc
true
=> nil

Or for a Method:

class A
  def i_am_method
    "Call me sometime..."
  end
end

> m = A.new.method(:i_am_method)
> m.class
Method
> m.is_a? Object
true
> m.call
"Call me sometime..."

And operators (like +, -, [], <<) are implemented as methods:

class String
  def +
    "I'm just a method!"
  end
end

For people coming into programming for the first time, what this means in a practical sense is that all the rules that you can apply to one kind of Object can be extended to others. You can think of a String, Array, Class, File or any Class that you define as behaving in much the same way. This is one of the reasons why Ruby is easier to pick up and work with than some other languages.

1
  • & unary operator converts the block into a Proc. But the block itself is not an object: {|n| n+1}.class # => SyntaxError: (irb):4: syntax error, unexpected '|', expecting '}' Mar 22, 2019 at 19:28
2

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

2

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.

1

Yup.

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

To see the chain of inheritance:

> pp Fixnum.ancestors
[Fixnum,
 Integer,
 Precision,
 Numeric,
 Comparable,
 Object,
  ...
 Kernel]
 => nil 
3
  • #ancestors gives all the included modules as well, not just the inheritance chain.
    – jtbandes
    Aug 7, 2010 at 7:40
  • Also, pp isn't available by default, at least in 1.8.7p174.
    – jtbandes
    Aug 7, 2010 at 7:42
  • Fixnum.is_a? Object (irb):9: warning: constant ::Fixnum is deprecated => true Because Fixnum and Bignum added into Integer Class.
    – S.Yadav
    Oct 26, 2018 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.