From: dblack@... Date: 2006-09-25T07:21:05+09:00 Subject: Re: How do I create an attribute accessor to do this... Hi -- On Mon, 25 Sep 2006, J2M wrote: > 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 | dblack@wobblini.net 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