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