From: tsela.cg@... Date: 2007-04-04T21:10:05+09:00 Subject: Re: Everything is a object? On Apr 4, 12:19 pm, Jamal Soueidan 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 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.