I have two methods defined in my ruby file.
def is_mandatory(string) puts xyz end def is_alphabets(string) puts abc end
An array containing the names of the methods.
methods = ["is_mandatory", "is_alphabets"]
When I do the following
methods.each do |method| puts method.concat("(\"abc\")") end
It just displays, is_mandatory("abc") is_alphabets("abc") rather than actually calling the method.
How can i convert the string to method name? Any help is greatly appreciated.
Cheers!!
The to_a() is an inbuilt method in Ruby returns an array containing the numbers in the given range. Syntax: range1.to_a() Parameters: The function accepts no parameter. Return Value: It returns an array containing all the numbers.
To convert a string in to function "eval()" method should be used. This method takes a string as a parameter and converts it into a function.
Ruby provides the to_i and to_f methods to convert strings to numbers. to_i converts a string to an integer, and to_f converts a string to a float.
The to_s function in Ruby returns a string containing the place-value representation of int with radix base (between 2 and 36). If no base is provided in the parameter then it assumes the base to be 10 and returns.
Best way is probably:
methods.each { |methodName| send(methodName, 'abc') }
See Object#send
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With