如何在类变量中定义的lambda/Proc中使用实例变量?

Srg*_*grn 4 ruby oop proc-object

我写了以下代码:

class Actions
  def initialize
    @people = []
    @commands = {
      "ADD" => ->(name){@people << name },
      "REMOVE" => ->(n=0){ puts "Goodbye" },
      "OTHER" => ->(n=0){puts "Do Nothing" }
    }
  end
  def run_command(cmd,*param)
    @commands[cmd].call param if @commands.key?(cmd)
  end
  def people
    @people
  end
end
act = Actions.new

act.run_command('ADD','joe')
act.run_command('ADD','jack')
puts act.people
Run Code Online (Sandbox Code Playgroud)

但是,当@commands哈希是一个类变量时,这是有效的,哈希中的代码不知道@people数组.

如何使@commands哈希成为类变量并仍然能够访问特定的对象实例变量?

mu *_*ort 6

您可以instance_exec在调用lambda时为其提供适当的上下文,查找注释以查看更改:

class Actions
  # Move the lambdas to a class variable, a COMMANDS constant
  # would work just as well and might be more appropriate.
  @@commands = {
    "ADD"    => ->(name)  { @people << name   },
    "REMOVE" => ->(n = 0) { puts "Goodbye"    },
    "OTHER"  => ->(n = 0) { puts "Do Nothing" }
  }
  def initialize
    @people = [ ]
  end
  def run_command(cmd, *param)
    # Use instance_exec and blockify the lambdas with '&'
    # to call them in the context of 'self'. Change the
    # @@commands to COMMANDS if you prefer to use a constant
    # for this stuff.
    instance_exec(param, &@@commands[cmd]) if @@commands.key?(cmd)
  end
  def people
    @people
  end
end
Run Code Online (Sandbox Code Playgroud)