Reputation: 25
Ok, this might be really crazy, and/or stupid but..
I am writing an ircbot in Ruby to learn the language and I want to include a dispatcher for commands in the bot.
So let's say that I have a hash that defines which command that belongs to what function:
hash = { ".voice" => "basic", ".op" => "basic" }
And then I do this:
hash.each_pair do |k,v|
case content[0]
when k then v(content[1])
end
end
Where content[0] is ".voice" and content[1] is the one being voiced.
This generates an error telling me that v is an undefined method for main:Object.
Is what I'm trying to do making any sense or is there a better way to do this? And if this way of doing this makes sense.. why does it return with that error?
Upvotes: 0
Views: 67
Reputation: 11198
Suppose you have a method, and the method's name in a string:
def basic(v)
puts v
end
method_name = 'basic'
If you do this:
method_name('Hello')
You'll get your error
undefined method `method_name' for main:Object (NoMethodError)
You have to make the string into a method object to be able to use it:
method_object = method(method_name)
method_object.call('Hello!')
Upvotes: 1