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).
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
class Array
alias __acc = unless method_defined? :__acc
def =(a, b)
__acc(a, b)
self
end
end
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