88

In Ruby, I can define a method foo=(bar):

irb(main):001:0> def foo=(bar)
irb(main):002:1>   p "foo=#{bar}"
irb(main):003:1> end
=> nil

Now I'd like to check if it has been defined,

irb(main):004:0> defined?(foo=)
SyntaxError: compile error
(irb):4: syntax error, unexpected ')'
 from (irb):4
 from :0

What is the proper syntax to use here? I assume there must be a way to escape foo= such that it is parsed and passed correctly to the defined? operator.

Cadoiz
  • 1,446
  • 21
  • 31
Alex Boisvert
  • 2,850
  • 2
  • 19
  • 18

2 Answers2

145

The problem is that the foo= method is designed to be used in assignments. You can use defined? in the following way to see what's going on:

defined?(self.foo=())
#=> nil
defined?(self.foo = "bar")
#=> nil

def foo=(bar)
end

defined?(self.foo=())
#=> "assignment"
defined?(self.foo = "bar")
#=> "assignment"

Compare that to:

def foo
end

defined?(foo)
#=> "method"

To test if the foo= method is defined, you should use respond_to? instead:

respond_to?(:foo=)
#=> false

def foo=(bar)
end

respond_to?(:foo=)
#=> true
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
molf
  • 73,644
  • 13
  • 135
  • 118
  • Thanks! This solves my problem. I'm still curious to know if there's a way to escape foo= such that it can be fed to defined? but at least now I can move on. – Alex Boisvert Feb 27 '10 at 19:24
  • 1
    The problem here is that `foo=` is always used in assignments, so Ruby will return `"assignment"` if you test for `defined? foo()` (see updated answer). – molf Feb 27 '10 at 19:40
50

You can check if a method exists by using the respond_to? method, and you pass it a symbol, e.g. bar.respond_to?(:foo=) to see if the object bar has a method foo=. If you want to know if instances of a class respond to a method you can use method_defined? on the class (or module), e.g. Foo.method_defined?(:bar=).

defined? isn't a method, but an operator which returns a description of the operand (or nil if it is not defined, which is why it can be used in an if statement). The operand can be any expression, i.e. a constant, a variable, an assignment, a method, a method call, etc. The reason why it doesn't work when you do defined?(foo=) is because of the parentheses, skip them and it should work more or less as expected. That being said, defined? is a pretty weird operator, and no one uses it to test for the existence of methods.

Theo
  • 131,503
  • 21
  • 160
  • 205
  • It is respond_to?, not responds_to? – 0x4a6f4672 Jul 30 '12 at 15:42
  • 3
    Ah, the spelling of Ruby core library... `respond_to?`, `start_with?`, `end_with?`. – Theo Jul 31 '12 at 10:00
  • 3
    http://stackoverflow.com/questions/5280556/why-does-ruby-use-respond-to-instead-of-responds-to has a nice explanation of the choice behind the naming TL;DR `you.knock if you.respond_to?(:knock)` – drewish Oct 23 '14 at 19:03