工作,我在initialize
方法使用eval()
:紅寶石二傳手不EVAL
class ActiveRecord::FakedModel
def initialize(attributes={})
attributes = {} if not attributes
attributes.each do |attr_name, value|
eval("@#{attr_name}=#{value.inspect}")
end
@attributes = attributes
end
...
end
,並有清洗空格二傳手:
class ContactForm < ActiveRecord::FakedModel
attr_accessor :plz
def plz=(plz)
@plz = plz.try(:delete,' ')
end
...
end
但此setter不能當我工作在散列中給出'plz':
c=ContactForm.new(:plz=>' 1 3 3 3 ')
=> #<ContactForm:0x1024e3a10 @attributes={:plz=>" 1 3 3 3 "}, @plz=" 1 3 3 3 ">
使用setter在有什麼問題?
這是一個很好的提示,但不是一個真正的答案的問題,這是不被調用的二傳手。 –