Everything is a object?

Hello,

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

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 :slight_smile:

Thanks :smiley:

Jamal

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 :slight_smile:

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

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 :slight_smile:

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 ?

Jamal S. [email protected] 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

Thanks :smiley:

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

Jamal S. wrote:

Thanks :smiley:

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

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/

Gary W.

Jamal S. wrote:

Thanks :smiley:

defined?

which class have that method?

I cannot find it in the documentation page?

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 :slight_smile:

if var.nil?

this didn’t work either …

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

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 :stuck_out_tongue: 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.

Jamal S. wrote:

so in Ruby everything is object if it was declared before :stuck_out_tongue: 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!

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?

Devin M. wrote:

Jamal S. wrote:

so in Ruby everything is object if it was declared before :stuck_out_tongue: 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 :slight_smile:

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 :slight_smile:

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 :slight_smile: 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

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

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.

:smiley:

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’

On Apr 4, 12:19 pm, Jamal S. [email protected] 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 :slight_smile:

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
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 :slight_smile:

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 :slight_smile: 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.

On Apr 4, 2007, at 7:10 AM, [email protected] 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. :wink:

James Edward G. II

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 :smiley: I love it :smiley:

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

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.

:smiley:

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

On Apr 4, 2:43 pm, Jamal S. [email protected] 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.

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