From: Mark Probert Date: 2002-02-23T04:19:19+09:00 Subject: dispatching and class references Thank you for everyone for helping me with my dispatch query the other day. Now fro part 2 :-) It seems that ruby returns object values by reference. Is there any way to return the value? I would like the pattern, stored in Sym::val to be treated as a template and retain its value ('xxx') over each dispatch call. So: class Sym attr_reader :sym, :val def initialize(sym, val) @sym = sym @val = val end def to_s "sym = #{@sym} val=#{@val}" end end class Bob @@dtbl = { 'a' => Sym.new(:do_a, 'this is a string: xxx'), 'b' => Sym.new(:do_b, 'so is this: xxx') } def do_a(msg=nil) puts "a --> #{msg}" end def do_b(msg=nil) puts "b --> #{msg}" end def dispatch(ref=nil) if @@dtbl[ref] obj = @@dtbl[ref] s = obj.val s.sub!(/xxx/, '123') send obj.sym, obj.val else puts "--> unknown ref (#{ref})" end end def prntbl @@dtbl.keys.each { |k| puts @@dtbl[k] } end end irb(main):001:0> b = Bob.new # irb(main):002:0> b.prntbl sym = do_a val=this is a string: xxx sym = do_b val=so is this: xxx ["a", "b"] irb(main):003:0> b.dispatch('a') a --> this is a string: 123 nil irb(main):004:0> b.prntbl sym = do_a val=this is a string: 123 # <-- the sub! has changed obj.val sym = do_b val=so is this: xxx ["a", "b"] Regards, -mark.