# Everything is a object?

**URL:** <https://www.ruby-forum.com/t/everything-is-a-object/91040>\
**Category:** Ruby\
**Created:** [April 4, 2007, 12:49am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040 "2007-04-04T00:49:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 12:49am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/1 "2007-04-04T00:49:04Z")

</div>

Hello,

I understood everything is a object, and the base class for all objects  
is object 🙂

so when I try to write just that this line below in my code i get an  
error?

puts just\_something.class.to\_s

undefined local variable or method ‘just\_something’ for main:object

why does this happend?

I cannot even write this below

puts ‘nil’ unless just\_something.nil?

hope someone can help me understand whats wrong 🙂

Thanks 😃

Jamal

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 12:57am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/2 "2007-04-04T00:57:25Z")

</div>

On Wed, Apr 04, 2007 at 07:49:05AM +0900, Jamal S. wrote:

> why does this happend?
> 
> I cannot even write this below
> 
> puts ‘nil’ unless just\_something.nil?
> 
> hope someone can help me understand whats wrong 🙂

Basically, everything _that exists_ in Ruby is an object. If it doesn’t  
exist yet, it can’t be an object. You have to have a “just\_something”  
before it can be an object. You could try creating a “just\_something”  
with an assignment, for instance:

irb(main):001:0\> just\_something = 0  
=\> 0  
irb(main):002:0\> puts just\_something.class.to\_s  
Fixnum  
=\> nil

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 1:17am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/3 "2007-04-04T01:17:22Z")

</div>

Chad P. wrote:

> On Wed, Apr 04, 2007 at 07:49:05AM +0900, Jamal S. wrote:
> 
> > why does this happend?
> > 
> > I cannot even write this below
> > 
> > puts ‘nil’ unless just\_something.nil?
> > 
> > hope someone can help me understand whats wrong 🙂
> 
> Basically, everything _that exists_ in Ruby is an object. If it doesn’t  
> exist yet, it can’t be an object. You have to have a “just\_something”  
> before it can be an object. You could try creating a “just\_something”  
> with an assignment, for instance:
> 
> irb(main):001:0\> just\_something = 0  
> =\> 0  
> irb(main):002:0\> puts just\_something.class.to\_s  
> Fixnum  
> =\> nil

how can I check if its not a object because then its empty and not  
created ?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 1:42am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/4 "2007-04-04T01:42:27Z")

</div>

Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how can I check if its not a object because then its empty and not  
> created ?

If it’s empty that means it has been created but with a nil value. You  
have to check if its defined in the current scope:

irb(main):005:0\> defined? a  
=\> nil  
irb(main):006:0\> a  
NameError: undefined local variable or method `a’ for main:Object  
from (irb):6  
from :0

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 2:06am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/5 "2007-04-04T02:06:11Z")

</div>

Thanks 😃

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 2:31am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/6 "2007-04-04T02:31:30Z")

</div>

On Apr 3, 2007, at 8:09 PM, Jamal S. wrote:

> Jamal S. wrote:
> 
> > Thanks 😃
> 
> defined?

It is a language keyword, not a method, so you won’t  
find it in any of the class documentation. You’ll have  
to look at one of the language manuals. It is  
discussed here:

[http://www.rubycentral.com/book/tut\_expressions.html](http://www.rubycentral.com/book/tut_expressions.html)

I’m not sure If that is a helpful direction for you  
though based on your previous questions.

It seems like you try working with some of the  
tutorials. There are some good links for getting  
started with Ruby at:

[http://www.ruby-lang.org/en/documentation/](http://www.ruby-lang.org/en/documentation/)

Gary W.

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 2:09am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/7 "2007-04-04T02:09:51Z")

</div>

Jamal S. wrote:

> Thanks 😃

defined?

which class have that method?

I cannot find it in the documentation page?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 2:39am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/8 "2007-04-04T02:39:54Z")

</div>

Gary W. wrote:

> It is a language keyword, not a method, so you won’t  
> find it in any of the class documentation. You’ll have  
> to look at one of the language manuals. It is  
> discussed here:
> 
> Gary W.

From my previous language (PHP) I could easily write function like this

if (empty($var))

even if the $var is not declared…

I thought in Ruby it will work the same way

if var.empty? (but empty is only in the String) so I thought something  
like this would solve it.

if var.to\_s.empty? (object to string)

this didn’t work either, but object had the nil method which could be  
used 🙂

if var.nil?

this didn’t work either …

so in Ruby everything is object if it was declared before 😛 o

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 3:29am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/9 "2007-04-04T03:29:32Z")

</div>

On Apr 3, 2007, at 8:39 PM, Jamal S. wrote:

> From my previous language (PHP) I could easily write function like  
> this
> 
> if (empty($var))
> 
> even if the $var is not declared…
> 
> I thought in Ruby it will work the same way

Ruby does not have an analogous concept to PHP’s empty.

Ruby’s ‘defined?’ is closer to PHP’s ‘isset’ (for variables) or  
‘defined’ (for constants).

You have to be careful with analogies between PHP and Ruby because  
the languages don’t treat variables, data and objects in the same  
way at all. For example, PHP’s function ‘is\_object(var)’ is  
superfluous in Ruby since if a variable is defined it must refer  
to an object–there are no other ‘things’ that a variable can  
reference. In PHP a basic string is _not_ an object. Ruby does  
not have that type of distinction.

The various tests for Ruby objects that would match PHP’s empty  
function:

```
     "".empty? # true for an empty string
     "x".empty? # false
     "0".empty? # false (strings aren't auto converted )
     "0".to_int.zero? # true

     0.zero? # true
     1.zero? # false

     nil.nil? # yes, nil is the nil object
     0.nil? # no, zero is not nil object

     x = false # make x refer to the false object
     x == false # true, x refers to the false object
     x == true # false, x doesn't refer to the false object
     x = true # x now refers to the false object
     x == false # false, x doesn't refer to the false object
     x == true # true, x does refer to the false object

     [].empty? # true, the array is empty
     [100].empty? # false, the array is not empty

```

In a boolean context false and nil are the only objects that are  
considered false. All other objects (0, an empty array, an empty  
string) are considered true:

if x

# x is neither nil nor false

else

# x must be nil or false

end

If you really wanted to match PHP’s empty function you would have to do  
something like:

def empty(obj)  
[nil, 0, false, [], “”, “0”].include?(obj)  
end

But that still won’t work for variables that are not defined. In  
practice the need to decide if a variable is defined or not is rare  
though.

> so in Ruby everything is object if it was declared before 😛 o

As with most absolutes the phrase ‘everything is an object’ is only  
true within a particular context. In this case with respect to  
data. All data in Ruby is accessed and manipulated in the context  
of objects and their methods. But variables are not data in Ruby.  
Variables reference objects but aren’t themselves objects. Other  
parts of the language can be accessed and manipulated as objects  
(data) though: classes, modules, methods, and more.

Gary W.

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 4:38am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/10 "2007-04-04T04:38:36Z")

</div>

Jamal S. wrote:

> so in Ruby everything is object if it was declared before 😛 o  
> I think perhaps the more accurate, though less sexy, wording is “every  
> expression that returns returns an object.”\*

For example: ‘rescue’ is not an object, it’s a keyword.  
‘begin…rescue…end’ is an expression, and hence has a return value, and  
that return value is an object.

In your case: ‘as\_yet\_undefined\_variable’ is an expression, too. In the  
case of a bare word, Ruby’s built-in behavior is to:

1. check for a local variable binding
2. failing that, send a message to self

#1 is failing, so it sends the as\_yet\_undefined\_variable message to  
self. If the method were defined, it would invoke it. Since it’s not  
defined, it calls method\_missing on self.

This is all built-in, unoverrideable behavior (AFAIK).

The default implementation of method\_missing (which you _can_ override)  
raises a NoMethodError exception. The exception is an object, but the  
act of “raising” it triggers some built-in behavior to fail fast. This  
is why I say “every expression that returns” instead of “every  
expression”. This is one case of an expression that doesn’t return.

This is all more complicated than the cute “Everything is an object,”  
but it’s also far more useful, and in my opinion, pretty intuitive.\*\*

I Might Be Wrong.

Of course, I’m guessing Gary’s email is more useful to you, but I don’t  
know PHP, and I’m a pedant.

Also, if you find yourself using defined? more than once a year, you  
might wanna post some code to ruby-talk for constructive criticism.\*\*\*

Devin

- Lowercase ‘o’ intentional – IIRC, evil.rb allows you to skirt the  
Object superclass thing.  
\*\* One exception being that you can’t always say `puts a unless (a = some_method).nil?`. (Note the single equals sign, vice double.)  
\*\*\* I’m including you, Rails Core!

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/11 "2007-04-04T12:02:47Z")

</div>

> On Apr 3, 2007, at 8:39 PM, Gary W. wrote:

I have been reading about these various boolean returns, how Ruby look  
at them, as you mentioned false and nil is the only objects which are  
considered false.

But I thought you did not need to define/declare any variables before  
using it, but this has been misunderstood, you should somehow assign the  
variable to something before trying to use it, since Ruby “Still” don’t  
know what object it is, right?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 12:19pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/12 "2007-04-04T12:19:21Z")

</div>

Devin M. wrote:

> Jamal S. wrote:
> 
> > so in Ruby everything is object if it was declared before 😛 o  
> > I think perhaps the more accurate, though less sexy, wording is “every  
> > expression that returns returns an object.”\*
> 
> For example: ‘rescue’ is not an object, it’s a keyword.  
> ‘begin…rescue…end’ is an expression, and hence has a return value, and  
> that return value is an object.

I thought they are methods 🙂

I thought keywords are class, def, end, which mean something to the  
compiler only.

I did not think there would be something that behaved like a method  
“defined?” which would be keyword?

Why not just make it a global method, put it in a module so nobody is  
confused about that, because it looks like a method to me 🙂

1 - so keywords can act as methods?  
2 - keywords are build in into the language itself (Ruby)?  
3 - maybe keyword are just a name, keywords can also act as methods? but  
you can’t create them, number 2.

> Also, if you find yourself using defined? more than once a year, you  
> might wanna post some code to ruby-talk for constructive criticism.\*\*\*

Actually, I was just trying various objects from the Ruby documentation  
page, just to get a idea about the methods which I can use 🙂 but the  
code below let me think about “everything is object” and I thought that  
must be wrong, since the code below is not working :S

if a.nil?  
p ‘thanks’;  
end

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/13 "2007-04-04T12:37:21Z")

</div>

On 4 Apr 2007, at 11:02, Jamal S. wrote:

> I have been reading about these various boolean returns, how Ruby look  
> at them, as you mentioned false and nil is the only objects which are  
> considered false.
> 
> But I thought you did not need to define/declare any variables before  
> using it, but this has been misunderstood, you should somehow  
> assign the  
> variable to something before trying to use it, since Ruby “Still”  
> don’t  
> know what object it is, right?

The point is that you don’t have to define a variable before you make  
an assignment to it, much like in older variants of BASIC. Assignment  
implicitly declares the variable to exist within the specified scope.  
However until you make an assignment the interpreter is not aware of  
the variable’s existence (because logically it does not yet exist)  
and so attempts to reference it will fail to produce a reference to  
an object. An attempt will also be made to apply the token to the  
current object as a method, and if this too fails then the  
interpreter has no choice but to flag an error condition because  
semantically your program is broken - it makes no sense.

When you use defined? all you are essentially doing is asking the  
interpreter whether or not the given token references an object  
within the current scope.

Whilst the interpreter could be implemented in such a way that every  
unknown token returned a nil object if it was unknown to the  
interpreter, this would break the principle of least surprise. Think  
of this as equivalent to NaN (not a number) in standard floating-  
point libraries.

Ellie

## Eleanor McHugh Games With Brains

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/14 "2007-04-04T14:43:13Z")

</div>

Eleanor McHugh wrote:

> On 4 Apr 2007, at 11:02, Jamal S. wrote:
> 
> > I have been reading about these various boolean returns, how Ruby look  
> > at them, as you mentioned false and nil is the only objects which are  
> > considered false.
> > 
> > But I thought you did not need to define/declare any variables before  
> > using it, but this has been misunderstood, you should somehow  
> > assign the  
> > variable to something before trying to use it, since Ruby “Still”  
> > don’t  
> > know what object it is, right?
> 
> The point is that you don’t have to define a variable before you make  
> an assignment to it, much like in older variants of BASIC. Assignment  
> implicitly declares the variable to exist within the specified scope.  
> However until you make an assignment the interpreter is not aware of  
> the variable’s existence (because logically it does not yet exist)  
> and so attempts to reference it will fail to produce a reference to  
> an object. An attempt will also be made to apply the token to the  
> current object as a method, and if this too fails then the  
> interpreter has no choice but to flag an error condition because  
> semantically your program is broken - it makes no sense.

😃

So until I assign something to it, the interpreter would know nothing  
about what object it is.

In C# you can declare every variable what type it is like this:  
String mystring;

I assume I could do that in Ruby also:  
@var = String.new

But no need since Ruby would take care of this automatic when you assign  
something to variables.

When I write attributes in my code:  
attr\_assoccer :var

I assume \> then \< the interpreter knows only that @var is a object (base  
class)?

So before I can use @var.

- I can either declare it, so the interpreter knows something about it  
(base class object)  
OR
- I can assign something to it, so the interpreter knows what type it is

@var = ‘string’

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/15 "2007-04-04T14:11:17Z")

</div>

On Apr 4, 12:19 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > For example: ‘rescue’ is not an object, it’s a keyword.  
> > ‘begin…rescue…end’ is an expression, and hence has a return value, and  
> > that return value is an object.
> 
> I thought they are methods 🙂

They aren’t. They are reserved keywords from the language.

> I thought keywords are class, def, end, which mean something to the  
> compiler only.

Indeed, and so are begin, rescue, defined?, if, else, and a few  
others. See  
[http://ruby-doc.org/docs/ProgrammingRuby/html/language.html#S3](http://ruby-doc.org/docs/ProgrammingRuby/html/language.html#S3)  
for a list of the reserved keywords in Ruby. But I don’t think your  
view of keywords as being things “which mean something to the compiler  
only” is correct. See keywords just as they are: reserved names that  
you may not use as method or variable names since they are already  
used by the language to mean something specific and unchangeable.

> I did not think there would be something that behaved like a method  
> “defined?” which would be keyword?

Why not? “alias” is another one of them. And anyway, “defined?” does  
NOT behave like a method. If it did, it could NOT take a potentially  
unassigned variable as an argument.

> Why not just make it a global method, put it in a module so nobody is  
> confused about that, because it looks like a method to me 🙂

And how do you propose a method be implemented so that it recognise a  
variable is not assigned yet? It’s not possible! When Ruby executes a  
method, it first looks at its arguments, and tries to get their values  
BEFORE actually executing the method. If one of the arguments is an  
unassigned variable, it will fail at this point, and the method will  
never have been run in the first place! What you want would only be  
possible if Ruby had Lisp-like macro facilities, but it doesn’t.

So defined? _has_ to be a keyword, because it does something specific  
which means something to the compiler only (to take your comparison  
again), something that _cannot_ be done with methods.

> 1 - so keywords can act as methods?

More like the contrary: methods can act like keywords. Like the method  
“define\_method” acts like the keyword “def”.

> 2 - keywords are build in into the language itself (Ruby)?

Yes. And I don’t see how it’s so surprising given plenty of languages  
(including PHP) usually have many more keywords than Ruby has (some  
have less, but they are less common).

> 3 - maybe keyword are just a name, keywords can also act as methods? but  
> you can’t create them, number 2.

You seem to have trouble wrapping your mind around the concept of  
keywords. Doesn’t PHP have things like “if” and “while” which are not  
functions or methods?

> > Also, if you find yourself using defined? more than once a year, you  
> > might wanna post some code to ruby-talk for constructive criticism.\*\*\*
> 
> Actually, I was just trying various objects from the Ruby documentation  
> page, just to get a idea about the methods which I can use 🙂 but the  
> code below let me think about “everything is object” and I thought that  
> must be wrong, since the code below is not working :S

It’s not wrong. It’s just that “everything” refers only to _existing_  
things at the moment the interpreter encounters them, which is logical  
(since when does “everything” refer to inexistent things anyway?).  
Variables themselves are not objects. They are labels, names given to  
objects, just like “Jamal S.” is just a name, a label used to  
refer to the person that is you.

Think about it a moment. Let’s say I ask you: “Please send Steve  
Somename a bunch of flowers.” You’ve never heard of Steve Somename  
before. It’s the first time I ever mention a “Steve Somename” to you.  
What would you do? You’d have no idea how to send this person flowers  
when you don’t know him, and this his address even less. You wouldn’t  
even know whether I’m pulling your leg by asking you to send flowers  
to a non existent person (the equivalent in Ruby of having a variable  
assigned to “nil”), because there is no way for you to know even that.  
So what would you do? You’d normally come back to me and ask about  
that “Steve Somename” person, why you should send him flowers and  
what’s his address. Well, that’s basically what Ruby does when it  
encounters a variable someone tries to use before it has been  
assigned: it fails, and comes back to the programmer asking what this  
variable is supposed to represent.

This whole “everything is an object” and “variables needn’t to be  
declared” is something different. Let’s take another example. Now let  
me ask you: “Please send Anne Othername a bunch of flowers, she’s been  
very helpful to us. And here’s her address.” You’ll be able to do it  
immediately. You might not know who Anne Othername is, but you have  
already got enough info to do what I need you to do. In Ruby terms,  
I’ve assigned an object to the variable, and Ruby can now do something  
with it. And note how I didn’t need to e-mail you five minutes before  
that request saying: “I’m going to refer to a person you’ve never met  
named Anne Othername. Please be ready for it.” You’d find it useless  
if I did that. That’s the equivalent of Ruby’s “variables needn’t be  
declared before they are assigned.”

> if a.nil?  
> p ‘thanks’;  
> end

Indeed, if a has not been given a value yet, it cannot be “nil” (“nil”  
_is_ a value, and an object), so this will fail.

Hope this helps.

Christophe.

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/16 "2007-04-04T15:02:12Z")

</div>

On Apr 4, 2007, at 7:10 AM, removed\_email\_address@domain.invalid wrote:

> only" is correct. See keywords just as they are: reserved names that  
> you may not use as method or variable names since they are already  
> used by the language to mean something specific and unchangeable.

I don’t think that’s a good definition:

> > class Example  
> > def begin  
> > “I’m a method, not the keyword!”  
> > end  
> > end  
> > =\> nil
> 
> > Example.new.begin  
> > =\> “I’m a method, not the keyword!”

Ruby’s a pretty clever girl. 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/17 "2007-04-04T15:19:46Z")

</div>

unknown wrote:

> Yes. And I don’t see how it’s so surprising given plenty of languages  
> (including PHP) usually have many more keywords than Ruby has (some  
> have less, but they are less common).

They have plenty, but they don’t have something like defined?, they have  
functions that act as defined? keyword.

> Think about it a moment. Let’s say I ask you: "Please send Steve  
> Somename a bunch of flow…

Great example 😃 I love it 😃

> This whole “everything is an object” and “variables needn’t to be  
> declared” is something di…

Well someone should know her before me, to tell me about her, so  
basically someone knows about her?

> > if a.nil?  
> > p ‘thanks’;  
> > end
> 
> Indeed, if a has not been given a value yet, it cannot be “nil” (“nil”  
> _is_ a value, and an object), so this will fail.

That made me confused a little bit, How can nil be a class? and then a  
method in object base class (nil?)

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/18 "2007-04-04T14:59:38Z")

</div>

On Apr 4, 2007, at 7:43 AM, Jamal S. wrote:

> > > using it, but this has been misunderstood, you should somehow  
> > > and so attempts to reference it will fail to produce a reference to  
> > > an object. An attempt will also be made to apply the token to the  
> > > current object as a method, and if this too fails then the  
> > > interpreter has no choice but to flag an error condition because  
> > > semantically your program is broken - it makes no sense.
> 
> 😃
> 
> So until I assign something to it, the interpreter would know nothing  
> about what object it is.

I think it’s probably better to say that until you assign it, it  
doesn’t exist. But yes, you are on the right track here.

> In C# you can declare every variable what type it is like this:  
> String mystring;
> 
> I assume I could do that in Ruby also:  
> @var = String.new
> 
> But no need since Ruby would take care of this automatic when you  
> assign  
> something to variables.

C# cares what type a variable is. It uses that information for  
things like method lookup. Ruby does not. A variable can hold  
whatever you like:

str = String.new

# …

str = 5 # Ruby doesn’t care that it’s not a String now

This is called “dynamic typing” and is one of the great features of  
Ruby. It does take some getting use to though, if you are joining us  
from a statically typed language.

> When I write attributes in my code:  
> attr\_assoccer :var
> 
> I assume \> then \< the interpreter knows only that @var is a object  
> (base  
> class)?
> 
> So before I can use @var.

Actually, the it doesn’t exist until you define it rule only applies  
to local variables:

$ ruby -e ‘p local\_var’  
-e:1: undefined local variable or method `local\_var’ for main:Object  
(NameError)  
$ ruby -e ‘p @inst\_var’  
nil  
$ ruby -e ‘p $global\_var’  
nil

However, the last two issue warnings when enabled (which I recommend):

$ ruby -we ‘p @inst\_var’  
-e:1: warning: instance variable @inst\_var not initialized  
nil  
$ ruby -we ‘p $global\_var’  
-e:1: warning: global variable `$global\_var’ not initialized  
nil

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/19 "2007-04-04T15:30:58Z")

</div>

On Apr 4, 2:43 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So until I assign something to it, the interpreter would know nothing  
> about what object it is.

Of course not, the interpreter cannot read your mind.

> In C# you can declare every variable what type it is like this:  
> String mystring;
> 
> I assume I could do that in Ruby also:  
> @var = String.new

Except that when you’re doing that you’re not declaring what type of  
variable @var is, you’re just assigning a new empty string to @var  
( it’s the equivalent of @var = “”). That’s a big difference between  
Ruby and C#: in Ruby you _cannot_ declare a variable, because that  
does not mean anything (variables in Ruby don’t have “types” attached  
to them, as in C# and other statically typed languages, so declaring  
them would be meaningless).

> But no need since Ruby would take care of this automatic when you assign  
> something to variables.

Yes.

> When I write attributes in my code:  
> attr\_assoccer :var

I assume you mean:

attr\_accessor :var

> I assume \> then \< the interpreter knows only that @var is a object (base  
> class)?

No. “attr\_accessor” only defines two instance methods within the class  
you’re using it in, which allow @var (which is a private instance  
variable) to be accessible from outside its object. In other words,

class SomeClass

attr\_accessor :var

end

is exactly equivalent to:

class SomeClass

def var  
@var  
end

def var=(param)  
@var = param  
end  
end

Both allow you to do:

instance = SomeClass.new  
instance.var = “Hello world!”  
puts instance.var

However, what happens is that when you try to use an instance variable  
that has never been assigned, Ruby gives it the “nil” value  
automatically. Exactly why it does that with instance variables and  
not with local variables I don’t know, but they are used in different  
enough ways that the difference should not be bothersome.

> So before I can use @var.
> 
> - I can either declare it, so the interpreter knows something about it  
> (base class object)

Except that you can’t declare it. At most you can assign to it some  
object value. Nothing prevents you from assigning it to a different  
and unrelated object value later (it can be of any class!).

> OR
> 
> - I can assign something to it, so the interpreter knows what type it is
> 
> @var = ‘string’

That is actually the only thing you can do, since declaration don’t  
exist in Ruby. However, for instance variables (variables with a @ in  
front of them), you needn’t do that, as Ruby gives them the nil value  
automatically. All the other variable types (local variables,  
constants and class variables) need to be assigned to before use.

Christophe.

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 4, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/20 "2007-04-04T15:33:58Z")

</div>

On Apr 4, 2007, at 8:25 AM, Jamal S. wrote:

> Actually I was trying to do it this way, when I started.
> 
> if @var.nil?  
> p “empty”  
> end
> 
> which works normal…but @var is nothing yet, and not declared, but  
> still  
> its object of object class?

I think you’re making this harder than it is. Unassigned instance  
(and global) variables default to nil, but issue a warning on first  
use. nil is an object like anything else in Ruby.

James Edward G. II

[Next page](https://www.ruby-forum.com/t/everything-is-a-object/91040.md?page=2)
