Ruby’s defined? Operator


Even if you’ve used Ruby’s defined? operator on a daily basis, you may not understand how it works. I sure didn’t until recently, but it’s worth a look.

A refresher in memoization etiquette

If you’re acquainted with memoization, this might look familiar:

class Person
  attr_accessor :first_name, :last_name

  def full_name
    return @full_name if defined?(@full_name)
    @full_name = "#{first_name} #{last_name}"
  end
end

The full_name method above uses memoization – the return value of the method is calculated just once, on the first call. It’s stored in the instance variable @full_name, and used for subsequent calls to the method. I first discovered this technique digging through the code base for Thoughtbot’s shoulda gem. I’ve used it hundreds of times, and never really questioned how the defined? operator works until recently.

Question mark?

Ruby methods can contain some non-alphanumeric characters like “!” and “?”, and rubyists take advantage of this to add readability to our code. Methods ending in “!” typically mean one of two things: the method is altering its receiver, or it’s going to complain loudly if it fails (usually by raising an exception). By the same convention, methods ending in “?” are asking a question, and the answer is usually boolean (yes/no).

The defined? method follows this convention…sort of. I always assumed it returned true/false, but that’s only half the story. If the object in question is defined, defined? gives you a string description of the object. This equates to “true” in any conditional arguments. If the object is not defined, it returns nil, which equates to “false”.

Test anything. Almost.

So defined? works in any boolean context, but it also provides a little more info. And it works on just about anything. Classes:

ruby-1.9.2-p0 > defined? Person
 => nil 
ruby-1.9.2-p0 > class Person
ruby-1.9.2-p0 ?>  end
 => nil 
ruby-1.9.2-p0 > defined? Person
 => "constant" 

It works on methods:

ruby-1.9.2-p0 > def bark
ruby-1.9.2-p0 ?>  puts "woof"
ruby-1.9.2-p0 ?>  end
 => nil 
ruby-1.9.2-p0 > defined? bark
 => "method" 

And of course it works on variables of all kinds:

ruby-1.9.2-p0 > defined? @@a
 => nil 
ruby-1.9.2-p0 > @@a = 'a'
 => "a" 
ruby-1.9.2-p0 > defined? @@a
 => "class variable" 
ruby-1.9.2-p0 > defined? @b
 => nil 
ruby-1.9.2-p0 > @b = 'b'
 => "b" 
ruby-1.9.2-p0 > defined? @b
 => "instance-variable" 
ruby-1.9.2-p0 > defined? c
 => nil 
ruby-1.9.2-p0 > c = 'c'
 => "c" 
ruby-1.9.2-p0 > defined? c
 => "local-variable" 

I even tried other operators, just on a whim. But of course, this was too much to hope for :)

ruby-1.9.2-p0 > defined?(+)
SyntaxError: (irb):1: syntax error, unexpected ')'
	from /Users/bellmyer/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'

defined? is an operator, not a method

Because you can enclose the object you want to check in parentheses (as in, defined?(@full_name)), you might be tempted to think it’s a method. It’s not, it’s a native operator. This is an important distinction, because it means defined? can’t be overridden:

ruby-1.9.2-p0 > x = 'test variable'
 => "test variable" 
ruby-1.9.2-p0 > defined? x
 => "local-variable" 
ruby-1.9.2-p0 > def defined? object
ruby-1.9.2-p0 ?>  puts "defined? has been overridden!"
ruby-1.9.2-p0 ?>  end
 => nil 
ruby-1.9.2-p0 > defined? x
 => "local-variable" 

I don’t get an error trying to override the operator with a method definition, but it doesn’t work, either. Honestly, Ruby is so permissive I half expected the override to work anyway! Another clue that you’re dealing with an operator is that it has no receiver. That’s why you give it the object, instead of calling it from a receiver, like the nil? method:

ruby-1.9.2-p0 > @x.nil?
 => true 
ruby-1.9.2-p0 > @x.defined?
NoMethodError: undefined method `defined?' for nil:NilClass
	from (irb):8
	from /Users/bellmyer/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'

While defined? is most valuable (and most commonly used) in a boolean context, there may be meta-programming applications where you’d want to what type of “thing” you’re dealing with. While you can always use the .class method, you have to already know that the object is defined. In the world of meta-programming, that’s often a luxury you don’t have.

About these ads

Tags: , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: