Calling dynamic methods in Ruby on Rails
To utilise dynamic method names in Ruby on Rails you can use Ruby's send method to pass a message to an object.
For example:
["foo", "bar"].each do |method|
MyClass.send(method)
end
The above is obviously a very crude example but it's the same as calling MyClass.foo and MyClass.bar.
The point is that the array could be dynamic and this means you can write code that calls methods prior to knowing what those methods may well be.
You can use send to call dynamic helper methods by just calling it on it's own.
About
Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.
Contact
my name at gmail.com
More snippets
Take a look in the archive
Need a website?
Contact my employer. Make sure to check out our portfolio of work.
Hosting
I recommend hostingrails.com
2 comments made
Awesome! Thanks much!
Thanks for the find. You should also mention that respond_to? can verify the presence of a dynamic method. (:
Got something to say?