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

发布于 2024-12-23 18:25:53 字数 628 浏览 3 评论 0原文

我编写了以下代码:

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

这是可行的,但是,当 @commands 哈希是类变量时,哈希内的代码不知道 @people 数组。

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

I wrote the following code:

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

This works, however, when the @commands hash is a class variable, the code inside the hash doesn't know the @people array.

How can I make the @commands hash be a class variable and still be able to access the specific object instance variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2024-12-30 18:25:53

您可以使用 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

You could use instance_exec to supply the appropriate context for the lambdas when you call them, look for the comments to see the changes:

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
你是我的挚爱i 2024-12-30 18:25:53

编辑遵循@VictorMoroz和@mu的建议:

class Actions
  def initialize
    @people = []
  end

  def cmd_add(name)
    @people << name
  end

  def cmd_remove
    puts "Goodbye"
  end

  def cmd_other
    puts "Do Nothing"
  end

  def people
    p @people
  end

  def run_command(cmd, *param)
    cmd = 'cmd_' + cmd.to_s.downcase
    send(cmd, *param) if respond_to?(cmd)
  end
end

act = Actions.new

act.run_command('add', 'joe')
act.run_command(:ADD, 'jill')
act.run_command('ADD', 'jack')

act.run_command('people') # does nothing

act.people

或者

class Actions
  ALLOWED_METHODS = %w( add remove other )

  def initialize
    @people = []
  end

  def add(name)
    @people << name
  end

  def remove
    puts "Goodbye"
  end

  def other
    puts "Do Nothing"
  end

  def people
    p @people
  end

  def run_command(cmd, *param)
    cmd = cmd.to_s.downcase
    send(cmd, *param) if ALLOWED_METHODS.include?(cmd)
  end
end

act = Actions.new

act.run_command('add', 'joe')
act.run_command(:add, 'jill')
act.run_command('add', 'jack')

act.run_command('people') # does nothing

act.people

EDIT Following @VictorMoroz's and @mu's recommendations:

class Actions
  def initialize
    @people = []
  end

  def cmd_add(name)
    @people << name
  end

  def cmd_remove
    puts "Goodbye"
  end

  def cmd_other
    puts "Do Nothing"
  end

  def people
    p @people
  end

  def run_command(cmd, *param)
    cmd = 'cmd_' + cmd.to_s.downcase
    send(cmd, *param) if respond_to?(cmd)
  end
end

act = Actions.new

act.run_command('add', 'joe')
act.run_command(:ADD, 'jill')
act.run_command('ADD', 'jack')

act.run_command('people') # does nothing

act.people

Or

class Actions
  ALLOWED_METHODS = %w( add remove other )

  def initialize
    @people = []
  end

  def add(name)
    @people << name
  end

  def remove
    puts "Goodbye"
  end

  def other
    puts "Do Nothing"
  end

  def people
    p @people
  end

  def run_command(cmd, *param)
    cmd = cmd.to_s.downcase
    send(cmd, *param) if ALLOWED_METHODS.include?(cmd)
  end
end

act = Actions.new

act.run_command('add', 'joe')
act.run_command(:add, 'jill')
act.run_command('add', 'jack')

act.run_command('people') # does nothing

act.people
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文