-1

Possible Duplicate:
How to turn a string into a method call?

Using Ruby 1.9, how does one call a method which is contained within a string. I have attempted the following:

self.'method_name'

Basically what I am attempting to do is call methods based on a common naming convention building on the current methods name. I would like to be be able to call

self."#{__method__}_path"

and give I am in method search have the code execute as if I was calling

self.search_path
Community
  • 1
  • 1
rudolph9
  • 8,021
  • 9
  • 50
  • 80

1 Answers1

3

Have you looked at send?

Something like:

ACTIONS = %w[foo bar]

def execute(action)
      return send("do_#{action}") if ACTIONS.include?(action)
      raise "Unexpected action"
end

Reference: http://ruby-doc.org/core-1.9.3/Object.html#method-i-send

hkf
  • 4,440
  • 1
  • 30
  • 44