From: Devin Mullins Date: 2007-04-05T09:42:28+09:00 Subject: Re: Everything is a object? Jamal Soueidan wrote: > I thought keywords are class, def, end, which mean something to the > compiler only. http://www.google.com/search?q=ruby+keywords The first result is comically direct. > 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 :) The reason why not is that when calling methods, Ruby always evaluates the arguments first.[1] So saying `defined? blahblah` would fail, because Ruby would try to evaluate blahblah, not find a blahblah method, and raise a NoMethodError. Now, you *could* make a defined method that took a String instead of a bare word, e.g.: def str_defined?(str) # cheap & ugly implementation eval "defined?(#{str})" # if you don't want to use defined? there are other hooks available: # constants, local_variables, instance_variables, class_variables, # global_variables # Those guys *are* methods, magically defined by the interpreter. end str_defined? "blahblah" There are method/keyword parallels in Ruby like this -- define_method vs def, alias_method vs alias, etc. You could even use blocks to make something that looked like: is_defined? { blahblah } But that's very much insane. > Ruby already know whatever start with @ would be instance of > something, if not then it would still be nil object, means false. "instance variable" does not mean "instance of something," but explaining what is *does* mean would mean a rather long side-wind into OOP. > Why does Ruby treat @myvar different from myvar? BTW, you can emulate the @myvar way: def method_missing(sym, *a, &b) (!a.empty? || b) ? super : nil end a #=> nil a = 5 a #=> 5 But, like, don't do that. Devin [1] This differs from other languages like Lisp and Io -- it probably comes from its C heritage. > if a.nil? > p 'thanks'; > end class You def self.method_missing(sym) puts "#{self}'re #{sym}." end end You.welcome