2
1.9.3-p194 :012 > b=[1,2,3];b.instance_variable_set :@internal, "rrr";
b.define_singleton_method :xx do; @internal; end
=> #<Proc:[email protected](irb):12 (lambda)>
1.9.3-p194 :013 > b
=> [1, 2, 3]
1.9.3-p194 :014 > b.xx
=> "rrr"
1.9.3-p194 :015 > b.define_singleton_method :"xx=(val)" do; @internal=val; end
=> #<Proc:[email protected](irb):15 (lambda)>
1.9.3-p194 :017 > b.xx="yy"
NoMethodError: undefined method `xx=' for [1, 2, 3]:Array
from (irb):17
from /home/sir/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
我想擁有作者的實例變量,但它不起作用。我究竟做錯了什麼?ruby define_singleton_method:「attr =(val)」不起作用
是的......我已經看到這個'attr_writer:v'是'def v =(value)的快捷方式; @ V =值;結束'紅寶石accessor手冊,所以我認爲它會工作 –