I heard that everything in ruby is object. I replied in an interview that a variable is an object, and the interviewer said NO. Anybody know the truth?
-
why this minus mark? I said the right answer. – shajin Dec 05 '11 at 07:32
-
1To whom should I ask, Intervier said 'No, in ruby variable is not an object'.So I want to know the truth.Consider me as newbie – shajin Dec 05 '11 at 07:34
-
@prusswan Pls refer the right answer – shajin Dec 05 '11 at 07:48
5 Answers
"In ruby, everything is an object" is basically true.
But more accurately, I would say that any value that can be assigned to a variable or returned from a method is an object. Is a variable an object? Not really. A variable is simply a name of an object (also known as a "pointer") that allows you locate it in memory and do stuff with it.
shajin = Person.new()
In this snippet, we have a variable shajin
, which points to an object (an instance of the person class). The variable is simply the identifier for an object, but is not the object itself.
I think it was a trick question. Ultimately object orientation is feature for humans to understand complex programs, but computers are not object oriented themselves. Drill down enough layers and objects cease to exist in any language.
So perhaps it's more fair to say: "In ruby, everything important is an object".

- 178,991
- 47
- 309
- 337
Why not go directly to the source? The Ruby Language Specification couldn't be more clear and obvious (emphasis added by me):
6.2 Variables
6.2.1 General description
A variable is denoted by a name, and refers to an object, which is called the value of the variable. A variable itself is not an object.

- 363,080
- 75
- 446
- 653
http://www.techotopia.com/index.php/Understanding_Ruby_Variables
"A variable in Ruby is just a label for a container. A variable could contain almost anything - a string, an array, a hash. A variable name may only contain lowercase letters, numbers, and underscores. A variable name should ideally make sense in the context of your program."

- 2,326
- 5
- 24
- 30
"We'll begin with the fact that Ruby is a completely
object-orientated language. Every value is an object (...)."(The Ruby Programming Language, Flanagan & Matsumoto, page 2).
Note this book, co-authored by the language creator, does not state "everything is an object".
a = 1
1 is an object, 'a' is a reference to the 1 object. If 'a' was an object on it's own, it would have an object_id of it's own. But:
1.object_id #=> 3
a.object_id #=> 3
Also, methods are not really objects (but you can turn them into objects if needed).
-
Is there a method that turns a method into an object or are you just saying it's trivial to refactor a ruby method into an object? – Roman Davis Sep 17 '14 at 08:48
-
1@RomanDavis Yes, there is a method that turns methods into an object. It's the method method. `obj = 1.method(:+)` – steenslag Sep 17 '14 at 08:59
-
That's really cool. I tried to edit it the part that explained that methods aren't really objects with a link to back it up because it confused me at first. If it's worth your time, you could change it yourself. – Roman Davis Sep 17 '14 at 09:04
@Alex Wayne and @Jörg W Mittag answears are correct, but I would like to add that "not everything" important is an object. Like method and block are not objects, but can be converted to objects, with method
method and proc respectively.

- 967
- 9
- 22