Hi --
I want to be able to do this to merge a hash into attribute bas on
instance bar in class foo;
bar = Foo.new
bar.bas << { :this => "that" }
Is there any way to define the accessor in Foo to be able to do this?
Here's one way. It involves assigning a hash to
@bar, and adding the
<< behavior to that hash (using a module).
require 'test/unit'
class Foo
attr_reader :bas
module UpdateAppender
def <<(other)
update(other)
end
end
def initialize
@bas = {}.extend(UpdateAppender)
end
end
class TestMerging < Test::Unit::TestCase
def test_1
f = Foo.new
f.bas << { :this => "that" }
assert_equal(f.bas.keys, [:this])
end
end
David
--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1]
http://www.manning.com/black | [3]
http://www.rubypowerandlight.com
[2]
http://dablog.rubypal.com | [4]
http://www.rubycentral.org