将proc存储在哈希内的数组中 [英] Storing a proc inside an array inside a hash
问题描述
我仍在从事文字冒险.我在使用/使用功能时遇到麻烦.它的目的是调用一个哈希,其中的键是使用的对象,内容包括一个数组.数组中的第一个元素是目标对象,第二个元素是Proc,如果该关系变成与use/with函数的参数匹配,则将执行该Proc.
I am still working on my text adventure. I am having trouble with the use/with function. It is meant to call a Hash in which the key is the used object and the content includes an array; the first element in the array is the target object, and the second a Proc that will be executed if that relation turns to match the arguments for the use/with function.
请,您可以澄清一下如何将代码块存储在哈希内部的数组中,以便以后可以根据组合的对象来调用它吗?
Please, may you clarify me how I can store a code block inside an array inside a hash so I can recall it later depending on the objects that are being combined?
这是我的使用函数,它使用将对象与 with 一起使用":
Here is my use function that takes "use object with with":
def use(object, with)
if INTERACTIONS[object][0] == with
INTERACTIONS[object][1]
end
end
这就是我定义关系的方式(到目前为止只有一种):
And this is how I defined the relations (so far there is just one):
INTERACTIONS = {"key" => ["clock", p = Proc.new{puts "You open the clock!"}]}
每当我键入
use key with clock
它只返回新的提示行.
推荐答案
您忘记了 .call
proc:
INTERACTIONS = {"key" => ["clock", Proc.new {puts "You open the clock!"}]}
def use(object, with)
if INTERACTIONS[object][0] == with
INTERACTIONS[object][1].call # procs need to be `call`ed :)
end
end
use("key", "clock") # => You open the clock!
这篇关于将proc存储在哈希内的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!