# \[YANQ\] - how to create/modify \[\]=

**URL:** <https://rubytalk.org/t/yanq-how-to-create-modify/3451>\
**Category:** ruby-talk\
**Created:** [9 December 2002 08:37 UTC](https://rubytalk.org/t/yanq-how-to-create-modify/3451 "2002-12-09T08:37:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [9 December 2002 08:37 UTC](https://rubytalk.org/t/yanq-how-to-create-modify/3451/1 "2002-12-09T08:37:38Z")

</div>

Hi Ruby friends,

I would like to modify or create own Array#[]= method to return the array.

Currently,

C:\family\ruby\>irb  
irb(main):001:0\> a = [1,2,3,4]  
[1, 2, 3, 4]  
irb(main):002:0\> a[1] = 6  
6 \<====== returns value inserted  
irb(main):003:0\> a  
[1, 6, 3, 4]

I would like a[1] = 6 to return array a itself, so I can do eg,

a = [1,2,3,4]  
p (a[1]=6).sort

I’m reading sir Dave’s online axe book so a chapter reference would be fine.  
You could also tell me how you search it (since I’ve search but cannot find  
it, maybe my query is not good enough).

Many thanks,  
-botp

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [9 December 2002 10:15 UTC](https://rubytalk.org/t/yanq-how-to-create-modify/3451/2 "2002-12-09T10:15:48Z")

</div>

> Hi Ruby friends,
> 
> I would like to modify or create own Array#= method to return the array.
> 
> […]
> 
> Many thanks,  
> -botp

Now this is strange: someone who doesn’t like modifying objects wants to modify  
classes instead!

Anyway, I thought it would be really easy, but I can’t seem to do it.

Cheers,  
Gavin

> **···**
>
> From: “Peña, Botp” [botp@delmonte-phil.com](mailto:botp@delmonte-phil.com)

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [9 December 2002 10:34 UTC](https://rubytalk.org/t/yanq-how-to-create-modify/3451/3 "2002-12-09T10:34:32Z")

</div>

> I would like to modify or create own Array#= method to return the array.

Be carefull with this, because you'll perhaps use some module which don't  
expect to have this result. Or someone will use a method written by you  
and will be surprised by this result. This can give bugs very diificult to  
find.

> You could also tell me how you search it

See #alias, something like

&nbsp;&nbsp;&nbsp;class Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias \_\_acc = unless method\_defined? :\_\_acc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def =(a, b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_acc(a, b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

Guy Decoux

---

<div class="post-metadata">

**Author:** ![WATANABE\_Hirofumi1](https://avatars.discourse-cdn.com/v4/letter/w/c2a13f/32.png) [@WATANABE\_Hirofumi1](https://rubytalk.org/u/WATANABE_Hirofumi1)\
**Post date:** [9 December 2002 10:42 UTC](https://rubytalk.org/t/yanq-how-to-create-modify/3451/4 "2002-12-09T10:42:33Z")

</div>

“Peña, Botp” [botp@delmonte-phil.com](mailto:botp@delmonte-phil.com) writes:

> I would like a[1] = 6 to return array a itself, so I can do eg,
> 
> a = [1,2,3,4]  
> p (a[1]=6).sort
> 
> I’m reading sir Dave’s online axe book so a chapter reference would be fine.  
> You could also tell me how you search it (since I’ve search but cannot find  
> it, maybe my query is not good enough).

module MyArray  
def =(x, y)  
super x, y  
self  
end  
end

a = [1,2,3,4]  
a.extend MyArray  
p((a[1]=6).sort) #=\> [1, 3, 4, 6]

> **···**
>
> –  
> eban

---

<div class="post-metadata">

**Author:** ![Bj1](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@Bj1](https://rubytalk.org/u/Bj1)\
**Post date:** [9 December 2002 10:35 UTC](https://rubytalk.org/t/yanq-how-to-create-modify/3451/5 "2002-12-09T10:35:26Z")

</div>

> > I would like to modify or create own Array#= method to return the array.
> > 
> > […]
> > 
> > Many thanks,  
> > -botp
> 
> Now this is strange: someone who doesn’t like modifying objects wants to modify  
> classes instead!
> 
> Anyway, I thought it would be really easy, but I can’t seem to do it.
> 
> Cheers,  
> Gavin

I’d probably do something in the vincinity of,  
class SArray \< Array  
def = (a, b)  
super(a, b)  
self  
end  
end

or perhaps:  
class Array  
def set\_element(a, b)  
self[a] = b  
self  
end  
end

altering the basic = behavior seems IMO like a pretty bad idea.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [9 December 2002 14:29 UTC](https://rubytalk.org/t/yanq-how-to-create-modify/3451/6 "2002-12-09T14:29:45Z")

</div>

Having a hash argument would be prettier - then you can say  
a.set({1=\>6, 2=\>5, 3=\>-1}).sort

martin

> **···**
>
> Bj [bjst01@student.bth.se](mailto:bjst01@student.bth.se) wrote:
> 
> > class Array  
> > def set\_element(a, b)  
> > self[a] = b  
> > self  
> > end  
> > end
> > 
> > altering the basic = behavior seems IMO like a pretty bad idea.
