19

"Everything is an object" was one of the first things I learned about Ruby, but in Peter Cooper's Beginning Ruby: From Novice to Professional, it is mentioned that "almost everything in Ruby is an object".

Can you give me some examples of things that are not objects in Ruby?

Yaser Sulaiman
  • 3,311
  • 2
  • 28
  • 25

4 Answers4

13

The most obvious one that jumps into my head would be blocks. Blocks can be easily reified to a Proc object, either by using the &block parameter form in a parameter list or by using lambda, proc, Proc.new or (in Ruby 1.9) the "stabby lambda" syntax. But on its own, they aren't objects.

Another example are operators.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • what about the parameters that are used in a function ??.. Ex in Hash[1,2,3,4] are 1,2,3,4 objects ? – vireshas Aug 22 '12 at 12:12
  • 1
    Yes, they are literal Fixnums. – hdgarrood Aug 27 '12 at 00:15
  • Operators are objects, too. They are object methods (which are also objects). Try `5.method(:+).is_a?(Object)` – Kyle Macey Oct 25 '16 at 20:51
  • @KyleMacey: I personally believe methods are *not* objects in Ruby. I actually used to believe differently, and if you go hunting, there are probably still answers by me on this very site that say so. But I now believe that methods are *not* objects, and `Method` objects are merely Reflection Proxies that *represent* methods, but aren't the methods themselves. I was bitten by that when I tried to implement a docstring-like system of storing method documentation inside the `Method` objects themselves. The problem was, that depending on how you obtained the `Method` object, you might get … – Jörg W Mittag Oct 25 '16 at 21:55
  • … different `Method` objects for the same method, so that the documentation you stored inside the one object could not be retrieved from the other. In other words, you get exactly the same problems you always get with proxy objects: confusion about identity. This has lead me to come to the conclusion that methods are *not* objects, `Method` objects not withstanding. It's similar to Java, I guess: classes are not first-class objects in Java, even though you can reflectively create a `java.lang.Class` object. – Jörg W Mittag Oct 25 '16 at 21:58
  • @JörgWMittag Interesting! Thanks for the info, I'll look into it and read up a bit more when I get a chance – Kyle Macey Oct 25 '16 at 23:36
4
  1. if
  2. else
  3. {
  4. }

general language constructs, etc...

I think pretty much everything else (including methods) are objects.

DanSingerman
  • 36,066
  • 13
  • 81
  • 92
  • note that puts and print are actually object methods – Kunok Dec 26 '15 at 08:46
  • I'm fairly sure that methods aren't actually objects unless they are reified by calling #method. `> 1.object_id == 1.object_id` `=> true` `> 1.method(:+).object_id == 1.method(:+).object_id` ` => false ` It appears that Method objects are proxies that point back to some construct that you can't actually get at with Ruby. – Huliax May 13 '19 at 15:35
1

After splitting the script into meaningful tokens by the lexer, everything is an object. Including classes. Even literal constants like 1 are objects. Some objects may have a syntax that is not purely OO (i.e. syntactic sugar) but that's mostly for easy manipulation more than anything. Blocks are not strictly objects though (but can as someone said be converted into one).

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
Keltia
  • 14,535
  • 3
  • 29
  • 30
0

In the case of variable assignment, i.e. product = 5 * 5 the variable is not an object... so add that to the list

davidpm4
  • 562
  • 1
  • 7
  • 22