If you are using ruby-1.9, you can use Object#public_send , which does what you want.
If you are using ruby-1.8.7 or earlier, you need to write your own Object#public_send
class Object def public_send(name, *args) unless public_methods.include?(name.to_s) raise NoMethodError.new("undefined method `#{name}' for \"#{self.inspect}\":#{self.class}") end send(name, *args) end end
Or you can write your own Object#public_method , which behaves like an Object#method , but only for public methods
class Object def public_method(name) unless public_methods.include?(name.to_s) raise NameError.new("undefined method `#{name}' for class `#{self.class}'") end method(name) end end
johannes
source share