4

次のコードを書きました。

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ハッシュをクラス変数にして、特定のオブジェクト インスタンス変数にアクセスできるようにするにはどうすればよいですか?

4

2 に答える 2

6

ラムダを呼び出すときにラムダに適切なコンテキストを提供するために使用できますinstance_exec。コメントを探して変更を確認します。

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
于 2012-01-03T22:02:29.293 に答える
1

編集@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
于 2012-01-03T21:44:14.113 に答える