ruby define_singleton_method :"attr=(val)" doesn't work
ruby
Solution
It should accept the arguments via the block, not via the method name:
b.define_singleton_method(:xx=) do |val|
@internal = val
end
Problem
``` 1.9.3-p194 :012 > b=[1,2,3];b.instance_variable_set :@internal, "rrr"; b.define_singleton_method :xx do; @internal; end => #<Proc:0x000000020f3d20@(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:0x0000000254aec8@(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>' ``` I want to have writer for instance variable, but it doesn't work. What am I doing wrong?