# How do I create an attribute accessor to do this

**URL:** <https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842>\
**Category:** Ruby\
**Created:** [September 25, 2006, 12:06am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842 "2006-09-25T00:06:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [September 25, 2006, 12:06am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842/1 "2006-09-25T00:06:44Z")

</div>

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?

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [September 25, 2006, 12:20am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842/2 "2006-09-25T00:20:45Z")

</div>

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?
> 
> Thanks,  
> James

class Hash  
alias :\<\<, :merge!  
end

class Foo  
attr :bas  
def initialize  
@bas = {}  
end  
end

Is that what you had in mind?

T.

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [September 25, 2006, 12:22am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842/3 "2006-09-25T00:22:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [September 25, 2006, 1:04am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842/4 "2006-09-25T01:04:50Z")

</div>

Thanks Trans, I will have a go tomorrow it is very late here in the UK  
🙂

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [September 25, 2006, 1:02am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842/5 "2006-09-25T01:02:50Z")

</div>

Thanks Trans, I will have a go tomorrow it is very late here in the UK  
🙂

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [September 25, 2006, 1:06am UTC](https://www.ruby-forum.com/t/how-do-i-create-an-attribute-accessor-to-do-this/69842/6 "2006-09-25T01:06:51Z")

</div>

Thank you both for such quick responses. I will give this a go tomorrow  
(v. late here in the UK).

David - thank you for all the time you take to help out us newbies, and  
thank you for RailsConf Europe too - fan-tas-tic!
