# How do I create an attribute accessor to do this

**URL:** <https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291>\
**Category:** ruby-talk\
**Created:** [24 September 2006 22:05 UTC](https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291 "2006-09-24T22:05:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![J2M](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@J2M](https://rubytalk.org/u/J2M)\
**Post date:** [24 September 2006 22:05 UTC](https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291/1 "2006-09-24T22:05:39Z")

</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:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [24 September 2006 22:20 UTC](https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291/2 "2006-09-24T22:20:15Z")

</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

&nbsp;&nbsp;class Hash  
&nbsp;&nbsp;&nbsp;&nbsp;alias :\<\<, :merge!  
&nbsp;&nbsp;end

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

Is that what you had in mind?

T.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [24 September 2006 22:21 UTC](https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291/3 "2006-09-24T22:21:05Z")

</div>

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).

&nbsp;&nbsp;&nbsp;require 'test/unit'

&nbsp;&nbsp;&nbsp;class Foo

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :bas

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module UpdateAppender  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def \<\<(other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;update(other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@bas = {}.extend(UpdateAppender)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class TestMerging \< Test::Unit::TestCase  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test\_1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f = Foo.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.bas \<\< { :this =\> "that" }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(f.bas.keys, [:this])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

David

> **···**
>
> On Mon, 25 Sep 2006, J2M wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![J2M](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@J2M](https://rubytalk.org/u/J2M)\
**Post date:** [24 September 2006 23:02 UTC](https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291/4 "2006-09-24T23:02:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J2M](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@J2M](https://rubytalk.org/u/J2M)\
**Post date:** [24 September 2006 23:03 UTC](https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291/5 "2006-09-24T23:03:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J2M](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@J2M](https://rubytalk.org/u/J2M)\
**Post date:** [24 September 2006 23:05 UTC](https://rubytalk.org/t/how-do-i-create-an-attribute-accessor-to-do-this/31291/6 "2006-09-24T23:05:45Z")

</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!
