251

I know in Ruby that I can use respond_to? to check if an object has a certain method.

But, given the class, how can I check if the instance has a certain method?

i.e, something like

Foo.new.respond_to?(:bar)

But I feel like there's gotta be a better way than instantiating a new instance.

0

12 Answers 12

393

I don't know why everyone is suggesting you should be using instance_methods and include? when method_defined? does the job.

class Test
  def hello; end
end

Test.method_defined? :hello #=> true

NOTE

In case you are coming to Ruby from another OO language OR you think that method_defined means ONLY methods that you defined explicitly with:

def my_method
end

then read this:

In Ruby, a property (attribute) on your model is basically a method also. So method_defined? will also return true for properties, not just methods.

For example:

Given an instance of a class that has a String attribute first_name:

<instance>.first_name.class #=> String

<instance>.class.method_defined?(:first_name) #=> true

since first_name is both an attribute and a method (and a string of type String).

7
  • 9
    method_defined? is the best solution because instance_methods changed between Ruby 1.8 and 1.9. 1.8 returns an array of strings and 1.9 returns an array of symbols. method_defined? takes a symbol in both 1.8 and 1.9.
    – Jason
    May 2, 2012 at 17:03
  • 2
    Not to mention that :instance_methods will create a new array every call and that :include? will then have to walk the array to tell. In contrast, :method_defined? will internally query the method lookup tables (one hash lookup that is in C) and let you know without creating any new objects.
    – Asher
    Jul 12, 2013 at 0:40
  • 4
    While there is at least one advantage when you use it like this, String.instance_methods(false).include? :freeze, the false argument can tell exactly whether freeze method is defined in String class or not. In this case it will return false because freeze method is inherited from Kernel module by String, but String.method_defined? :freeze will always return true, which can't help much with such a purpose.
    – ugoa
    Jul 23, 2013 at 8:23
  • 1
    @Bane you're confusing methods on the class with methods available on the instance. method_defined? must be used on the class (e.g Array), but you are trying to use it on instances (e.g [])
    – horseyguy
    Nov 9, 2013 at 14:55
  • @banister Absolutely correct. Thank you for the clarification, it makes perfect sense on reading it. :) I've removed my comment so as to not confuse.
    – Bane
    Nov 9, 2013 at 14:57
46

You can use method_defined? as follows:

String.method_defined? :upcase # => true

Much easier, portable and efficient than the instance_methods.include? everyone else seems to be suggesting.

Keep in mind that you won't know if a class responds dynamically to some calls with method_missing, for example by redefining respond_to?, or since Ruby 1.9.2 by defining respond_to_missing?.

1
  • 3
    Note that if you've got an instance in hand and you don't know it's class, you can do instance.class.method_defined? :upcase
    – jaydel
    Oct 20, 2016 at 12:55
29

Actually this doesn't work for both Objects and Classes.

This does:

class TestClass
  def methodName
  end
end

So with the given answer, this works:

TestClass.method_defined? :methodName # => TRUE

But this does NOT work:

t = TestClass.new
t.method_defined? : methodName  # => ERROR!

So I use this for both classes and objects:

Classes:

TestClass.methods.include? 'methodName'  # => TRUE

Objects:

t = TestClass.new
t.methods.include? 'methodName'  # => TRUE
3
  • 3
    For instances you can also use `instance.class.method_defined?'
    – Luksurious
    Oct 12, 2013 at 15:16
  • It depends on your version of ruby. This method above works for all versions including 1.8.7.
    – Alex V
    Oct 16, 2013 at 23:36
  • @Luksurious: I don't think that will work if you have mixins.
    – user1142217
    Jun 19, 2021 at 2:08
10

The answer to "Given a class, see if instance has method (Ruby)" is better. Apparently Ruby has this built-in, and I somehow missed it. My answer is left for reference, regardless.

Ruby classes respond to the methods instance_methods and public_instance_methods. In Ruby 1.8, the first lists all instance method names in an array of strings, and the second restricts it to public methods. The second behavior is what you'd most likely want, since respond_to? restricts itself to public methods by default, as well.

Foo.public_instance_methods.include?('bar')

In Ruby 1.9, though, those methods return arrays of symbols.

Foo.public_instance_methods.include?(:bar)

If you're planning on doing this often, you might want to extend Module to include a shortcut method. (It may seem odd to assign this to Module instead of Class, but since that's where the instance_methods methods live, it's best to keep in line with that pattern.)

class Module
  def instance_respond_to?(method_name)
    public_instance_methods.include?(method_name)
  end
end

If you want to support both Ruby 1.8 and Ruby 1.9, that would be a convenient place to add the logic to search for both strings and symbols, as well.

2
  • 1
    method_defined? is better :)
    – horseyguy
    Jul 21, 2010 at 23:31
  • @banister - oh, my, and I somehow missed it! xD Threw a +1 behind @Marc's answer, and added a link to make sure it isn't missed :)
    – Matchu
    Jul 22, 2010 at 1:05
5

Try Foo.instance_methods.include? :bar

3

Not sure if this is the best way, but you could always do this:

Foo.instance_methods.include? 'bar'
3

I think there is something wrong with method_defined? in Rails. It may be inconsistent or something, so if you use Rails, it's better to use something from attribute_method?(attribute).

"testing for method_defined? on ActiveRecord classes doesn't work until an instantiation" is a question about the inconsistency.

3

If you're checking to see if an object can respond to a series of methods, you could do something like:

methods = [:valid?, :chase, :test]

def has_methods?(something, methods)
  methods & something.methods == methods
end

the methods & something.methods will join the two arrays on their common/matching elements. something.methods includes all of the methods you're checking for, it'll equal methods. For example:

[1,2] & [1,2,3,4,5]
==> [1,2]

so

[1,2] & [1,2,3,4,5] == [1,2]
==> true

In this situation, you'd want to use symbols, because when you call .methods, it returns an array of symbols and if you used ["my", "methods"], it'd return false.

3
class Foo
 def self.fclass_method
 end
 def finstance_method
 end
end

foo_obj = Foo.new
foo_obj.class.methods(false)
=> [:fclass_method] 

foo_obj.class.instance_methods(false)
=> [:fclass_method] 

Hope this helps you!

0
2

klass.instance_methods.include :method_name or "method_name", depending on the Ruby version I think.

2

While respond_to? will return true only for public methods, checking for "method definition" on a class may also pertain to private methods.

On Ruby v2.0+ checking both public and private sets can be achieved with

Foo.private_instance_methods.include?(:bar) || Foo.instance_methods.include?(:bar)
1

On my case working with ruby 2.5.3 the following sentences have worked perfectly :

value = "hello world"

value.methods.include? :upcase

It will return a boolean value true or false.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.