1

当我遇到一个令人沮丧的问题时,我正在做家庭作业。该作业是 Ruby 元编程中的一个练习,目标是定义一个“attr_accessor_with_history”,它与“attr_accessor”做所有相同的事情,但还提供一个属性曾经存在的所有值的历史记录。这是作业中提供的代码以及我为完成作业而添加的一些代码:

    class Class

  def attr_accessor_with_history(attr_name)
    attr_name = attr_name.to_s
    attr_hist_name = attr_name+'_history'
    history_hash = {attr_name => []}

    #getter
    self.class_eval("def #{attr_name} ; @#{attr_name} ; end")
    #setter
    self.class_eval %Q{
      def #{attr_name}=(val)
        # add to history
        @#{attr_hist_name} = [nil] if @#{attr_hist_name}.nil?
        @#{attr_hist_name} << val
        history_hash[@#{attr_name}] = @#{attr_hist_name}

        # set the value itself
        @#{attr_name} = val
      end

      def history(attr) ; @history_hash[attr.to_s] ; end
    }
  end
end

class Foo
  attr_accessor_with_history :bar
  attr_accessor_with_history :crud
end
f = Foo.new     # => #<Foo:0x127e678>
f.bar = 3       # => 3
f.bar = :wowzo  # => :wowzo
f.bar = 'boo!'  # => 'boo!'
puts f.history(:bar) # => [3, :wowzo, 'boo!']
f.crud = 42
f.crud = "Hello World!"
puts f.history(:crud)

我想使用散列来存储不同属性的不同历史记录,但我无法在 setter 的 class_eval 语句中访问该散列。无论我如何尝试设置它,我似乎总是为 []= 方法获得 NoMethodError,因为“history_hash”以某种方式变成 NilClass 类型,或者发生 NameError,因为它将“history_hash”视为未定义的局部变量或方法。如何在 class_eval 语句中使用哈希?

4

1 回答 1

1

或发生 NameError 是因为它将“history_hash”视为未定义的局部变量或方法

我会说你不能,因为它一个局部变量,在你想要的上下文中是不可访问的。但是,为什么你甚至需要它?我有理由确定它在“我为完成分配而添加的一些代码”中,而不是原始分配代码(我假设它希望您存储@barin的历史@bar_history- 否则到底是attr_hist_name什么? )

我也对字符串评估感到不舒服;通常没有必要,而 Ruby 可以通过其强大的元编程工具做得更好。这是我的做法:

class Class
  def attr_accessor_with_history(attr_name)
    attr_setter_name = :"#{attr_name}="
    attr_getter_name = :"#{attr_name}"
    attr_hist_name = :"@#{attr_name}_history"
    attr_name = :"@#{attr_name}"

    self.class_eval do
      define_method(attr_getter_name) do
        instance_variable_get(attr_name)
      end

      define_method(attr_setter_name) do |val|
        instance_variable_set(attr_name, val)
        history = instance_variable_get(attr_hist_name)
        instance_variable_set(attr_hist_name, history = []) unless history
        history << val
      end
    end
  end
end

class Object
  def history(attr_name)
    attr_hist_name = :"@#{attr_name}_history"
    instance_variable_get(attr_hist_name)
  end
end

最后,由于它是猴子修补基类,我宁愿在需要的地方使用改进来添加它,但这对于分配来说可能是过大的。

于 2017-03-22T07:55:20.747 回答