[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Everything is a object?

Jamal Soueidan

4/3/2007 10:49:00 PM

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

Jamal

--
Posted via http://www.ruby-....

37 Answers

Chad Perrin

4/3/2007 10:57:00 PM

0

On Wed, Apr 04, 2007 at 07:49:05AM +0900, Jamal Soueidan wrote:
>
> 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 :)

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

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]

Jamal Soueidan

4/3/2007 11:17:00 PM

0

Chad Perrin wrote:
> On Wed, Apr 04, 2007 at 07:49:05AM +0900, Jamal Soueidan 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 ?

--
Posted via http://www.ruby-....

raims

4/3/2007 11:39:00 PM

0

Jamal Soueidan <jkhaledsoueidan@gmail.com> 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


--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair

Jamal Soueidan

4/4/2007 12:10:00 AM

0

Jamal Soueidan wrote:
> Thanks :D

defined?


which class have that method?

I cannot find it in the documentation page?

--
Posted via http://www.ruby-....

Gary Wright

4/4/2007 12:31:00 AM

0


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

> Jamal Soueidan wrote:
>> Thanks :D
>
> 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_expres...

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/docu...


Gary Wright




Jamal Soueidan

4/4/2007 12:40:00 AM

0

Gary Wright 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 Wright

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 :P o

--
Posted via http://www.ruby-....

Gary Wright

4/4/2007 1:29:00 AM

0


On Apr 3, 2007, at 8:39 PM, Jamal Soueidan 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 :P 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 Wright




Jamal Soueidan

4/4/2007 10:03:00 AM

0

> On Apr 3, 2007, at 8:39 PM, Gary Wright 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?



--
Posted via http://www.ruby-....

Eleanor McHugh

4/4/2007 10:36:00 AM

0

On 4 Apr 2007, at 11:02, Jamal Soueidan 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



tsela.cg

4/4/2007 12:06:00 PM

0

On Apr 4, 12:19 pm, Jamal Soueidan <jkhaledsouei...@gmail.com> 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/langua...
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 Soueidan" 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.