我有一些这样的红宝石代码:
my_hash = {
key1: "value",
key2: "value"
}
def key1
do_something
end
def key2
do_something_else
end
如您所见,键和方法具有相同的名称。我现在想将哈希键“转换”为方法名称为哈希键的方法调用
作为背景,我有一个 ncurses 菜单,其中哈希值是标签,我只传递键。And when a menu entry is selected I want to execute the correct method without too much coding overhead like figuring out in a if or case statement which entry was selected.
这有可能吗?如果是,我该怎么做?
这件事对我来说有点难以解释,我希望你明白我的意思。