0
H! 我有以下(sniped腳本)。我有一個有多個定義的類,我想遍歷這些定義,直到某些定義返回true,所以我想將它們放入一個數組並進行迭代,我的問題是,當我想通過它的類訪問該定義時,定義的名稱,所以我可以在while循環中使用它我不能這樣做,因爲它返回錯誤。任何想法如何做到這一點?Ruby - 任何爲定義做字符串替換的方法?
#...
class FetchHash
def get_img_eight(something)
#...
end
def get_img_seven(someting)
#...
end
def get_img_six(something)
#...
end
def get_img_five(something)
#...
end
end
get_cmds = [ "get_img_eight", "get_img_seven", "get_img_six", "get_img_five" ]
fetchme = FetchHash.new
for get_cmd in (get_cmds)
while my_ret_hash.nil? do
mynotworkingcmd = "fetchme.#{get_cmd}"
my_ret_hash = mynotworkingcmd(something)
break if my_ret_hash.nil?
end
end
#...
錯誤:
./test:85:in `block in <main>': undefined method `mynotworkingcmd' for main:Object (NoMethodError)
from ./test.rb:82:in `each'
from ./test:82:in `<main>'
線85
在這種狙擊相當於my_ret_hash = mynotworkingcmd(something)
使用'fetchme .public_send(get_cmd,東西)' –
可能的dupl [動態方法在Ruby中調用](http://stackoverflow.com/questions/17454992/dynamic-method-calling-in-ruby) –