Call method only if it exists

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


Is there some hidden Ruby/Rails-magic for simply calling a method only if it exists?

Lets say I want to call

resource.phone_number

but I don't know beforehand if resource responds to phone_number. A way to do this is

resource.phone_number if resource.respond_to? :phone_number

That's not all that pretty if used in the wrong place. I'm curious if something exists that works more along the lines of how try is used (resource.try(:phone_number)).

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

If you are not satisfied with the standard ruby syntax for that, you are free to:

class Object
  def try_outside_rails(meth, *args, &cb)
    self.send(meth.to_sym, *args, &cb) if self.respond_to?(meth.to_sym)
  end
end

Now:

resource.try_outside_rails(:phone_number)

will behave as you wanted.

Solution 2 - Ruby on-Rails

I would try defined? (http://ruby-doc.org/docs/keywords/1.9/Object.html#defined-3F-method). It seems to do exactly what you are asking for:

resource.phone_number if defined? resource.phone_number

Solution 3 - Ruby on-Rails

I know this is very old post. But just wanted to know if this could be a possible answer and whether the impact is the same .

resource.try(:phone_number) rescue nil

Thanks

Solution 4 - Ruby on-Rails

I can't speak for the efficiency, but something like...

Klass.methods.include?(:method_name) 

works for me in Rails 4

Solution 5 - Ruby on-Rails

If A.c is defined and A.a and A.b are not, you can do A.a rescue A.b rescue A.c and it will work like a charm. You will be breaking some silly rules, though.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFransView Question on Stackoverflow
Solution 1 - Ruby on-RailsAleksei MatiushkinView Answer on Stackoverflow
Solution 2 - Ruby on-RailsjmarceliView Answer on Stackoverflow
Solution 3 - Ruby on-RailsBhavyaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsStephenView Answer on Stackoverflow
Solution 5 - Ruby on-RailsnrooseView Answer on Stackoverflow