そのようなときはsendが使える
sendにはmethod名を文字列かシンボルで渡す
methodの引数は第2引数とする
class Salute
attribute_accessor :name
def hello
puts 'hello'
end
def goodbye
puts 'goodbye'
end
end
s = Salute.new(name)
name = case name
when 'kyo': :hello
when 'keyes': :goodbye
end
s.send(name)
No comments:
Post a Comment