The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Method Check: defined?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Method Check: defined? Posted: Dec 31, 2004 9:43 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Method Check: defined?
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

How would it be if, today, I blew you away with a Ruby keyword, huh? At any rate, I’ve decided to. Ga-shoom! (The wizard descends.)

So, defined? actually isn’t a method, as I normally uncover in a chilling episde of Method Check. But it does look like one, so no one will blame you if you’ve nailed it as a method in days of yore, what with the question mark and all. And you can dress it like a method, because Ruby’s parser allows its argument to be surrounded with parens.

 >> x = 1     
 => 1
 >> defined? x
 => "local-variable" 
 >> defined?( y )
 => nil

At heart, the defined? keyword is something of a hack, a rather rare anomaly in Ruby’s syntax. Since an undefined variable is a perfectly legal argument, the defined? keyword couldn’t be implemented as a method. Which means that this keyword is left out of Ri documentation. The Pickaxe groups it with operators. So, it often gets overlooked.

From eval.c, here’s a list of possible returns from defined?:

“expression”, “super”, “assignment”, “method”, “yield”, “self”, “nil”, “true”, “false”, “assignment”, “local-variable”, “local-variable(in-block)”, “global-variable”, “instance-variable”, “constant”, “class variable” and a few other assorted keywords and global variables (such as “$'“).

As it happens, defined? takes its argument and simply queries the symbol table to see if it is defined. If it is, you get a simple string of identification (as listed above.)

However, the Ruby parser handles the parens variation of defined? a bit differently. Simply because it allows for complex expressions to be evaluated. But this makes for madly syruprous mayhem!

Everything contained in the parens is parsed, a node tree is built, and all nodes are scanned for the validity of their “definition” within Ruby.

 >> defined?( class << String; def n; 18; end; end )
 => "expression" 
 >> String.n
 => 18
 >> defined?( class << NonExistent; def n; 18; end; end )
 => nil

Check that out. The first call to defined? parses the code, establishes that its a valid expression, then executes the code and returns "expression". The second call parses the code, finds an undefined class name of NonExistent and returns nil—meaning “undefined”.

So, defined? is a great way to perform some parser sanity without triggering a myriad of exceptions. My one gripe with defined? is that it will throw a SyntaxError if the expression in the parens is malformed. But, wouldn’t it be cool if that could be sniffed out as well??

Now, go. Play with it. I’ll bet you can come up with some crazy recipes.

Read: Method Check: defined?

Topic: Validate models using code, not annotations Previous Topic   Next Topic Topic: Santa Matz

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use