# Array#\[\]=

**URL:** <https://rubytalk.org/t/array/20745>\
**Category:** ruby-talk\
**Created:** [11 September 2005 18:01 UTC](https://rubytalk.org/t/array/20745 "2005-09-11T18:01:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [11 September 2005 18:01 UTC](https://rubytalk.org/t/array/20745/1 "2005-09-11T18:01:33Z")

</div>

I'm writing a class representing a coordinate system, and I want to be able to give a point in the system a value (eg. a label). The problem is that the system can have a variable number of dimensions. If it just had three, I could write:

&nbsp;&nbsp;&nbsp;def []= (x, y, z, value); end

But this being an N-dimensional coordinate system, I have to come up with something else. The most logical solution to me is this:

&nbsp;&nbsp;&nbsp;def[]= (\*coordinates, value); end

But this gives a syntax error. Am I way off base, or would it be useful if variable length argument lists could be written this way?

Daniel

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [11 September 2005 18:09 UTC](https://rubytalk.org/t/array/20745/2 "2005-09-11T18:09:03Z")

</div>

If the slurpy arrays could be used anywhere in the list, if becomes impossible to parse the arguments correctly.

In this case, you'll need to separate the value yourself:

def =( \*args )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = args.pop  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cooridinates = args  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
end

Hope that helps.

James Edward Gray II

> **···**
>
> On Sep 11, 2005, at 1:01 PM, Daniel Schierbeck wrote:
> 
> > I'm writing a class representing a coordinate system, and I want to be able to give a point in the system a value (eg. a label). The problem is that the system can have a variable number of dimensions. If it just had three, I could write:
> > 
> > &nbsp;&nbsp;def = (x, y, z, value); end
> > 
> > But this being an N-dimensional coordinate system, I have to come up with something else. The most logical solution to me is this:
> > 
> > &nbsp;&nbsp;def= (\*coordinates, value); end
> > 
> > But this gives a syntax error. Am I way off base, or would it be useful if variable length argument lists could be written this way?

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [11 September 2005 18:21 UTC](https://rubytalk.org/t/array/20745/3 "2005-09-11T18:21:35Z")

</div>

def =(\*a)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = a.pop  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coordinates = a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise ArgumentError if value.nil? or coordinate.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;end

hth.

-a

> **···**
>
> On Mon, 12 Sep 2005, Daniel Schierbeck wrote:
> 
> > I'm writing a class representing a coordinate system, and I want to be able to give a point in the system a value (eg. a label). The problem is that the system can have a variable number of dimensions. If it just had three, I could write:
> > 
> > def = (x, y, z, value); end
> > 
> > But this being an N-dimensional coordinate system, I have to come up with something else. The most logical solution to me is this:
> > 
> > def= (\*coordinates, value); end
> > 
> > But this gives a syntax error. Am I way off base, or would it be useful if variable length argument lists could be written this way?
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Devin\_Mullins](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Devin\_Mullins](https://rubytalk.org/u/Devin_Mullins)\
**Post date:** [11 September 2005 18:25 UTC](https://rubytalk.org/t/array/20745/4 "2005-09-11T18:25:49Z")

</div>

Daniel Schierbeck wrote:

> dimensions. If it just had three, I could write:
> 
> &nbsp;&nbsp;def = (x, y, z, value); end
> 
> But this being an N-dimensional coordinate system, I have to come up with something else. The most logical solution to me is this:
> 
> &nbsp;&nbsp;def= (\*coordinates, value); end
> 
> But this gives a syntax error. Am I way off base, or would it be useful if variable length argument lists could be written this way?

Yes, that would be useful. It has been discussed many-a-time, before, too. 🙂 For now, you'll have to implement your own solution in-method:  
def = \*args  
&nbsp;&nbsp;cords, value = args[0..-2],args[-1]  
&nbsp;&nbsp;#...  
end

Personally, I'd also like to see this:

def = x, y, value  
&nbsp;&nbsp;#...  
ordef x, y, z, value  
&nbsp;&nbsp;#...  
ordef \*args #gets here only if neither of the first two match  
&nbsp;&nbsp;#...  
end

But none of this exists. So live with it. 🙂

(I'd say, "Go write an RCR," but I can't imagine that 3 don't exist already.)

Devin

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [11 September 2005 20:53 UTC](https://rubytalk.org/t/array/20745/5 "2005-09-11T20:53:49Z")

</div>

Coincidentally, I was just writing an RCR for this one. It is  
#315. I'm also writing the corollary to this one which is to  
be able to call a method with more arguments after the splat  
(also applies to multi-assigns).

> **···**
>
> --- Daniel Schierbeck \<daniel.schierbeck@gmail.com\> wrote:
> 
> > I'm writing a class representing a coordinate system, and I  
> > want to be  
> > able to give a point in the system a value (eg. a label). The  
> > problem is  
> > that the system can have a variable number of dimensions. If  
> > it just had  
> > three, I could write:
> > 
> > &nbsp;&nbsp;&nbsp;def = (x, y, z, value); end
> > 
> > But this being an N-dimensional coordinate system, I have to  
> > come up  
> > with something else. The most logical solution to me is this:
> > 
> > &nbsp;&nbsp;&nbsp;def= (\*coordinates, value); end
> > 
> > But this gives a syntax error. Am I way off base, or would it  
> > be useful  
> > if variable length argument lists could be written this way?
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [11 September 2005 18:31 UTC](https://rubytalk.org/t/array/20745/6 "2005-09-11T18:31:32Z")

</div>

James Edward Gray II wrote:

> **···**
>
> > On Sep 11, 2005, at 1:01 PM, Daniel Schierbeck wrote:
> > 
> > > I'm writing a class representing a coordinate system, and I want to be able to give a point in the system a value (eg. a label). The problem is that the system can have a variable number of dimensions. If it just had three, I could write:
> > > 
> > > &nbsp;&nbsp;def = (x, y, z, value); end
> > > 
> > > But this being an N-dimensional coordinate system, I have to come up with something else. The most logical solution to me is this:
> > > 
> > > &nbsp;&nbsp;def= (\*coordinates, value); end
> > > 
> > > But this gives a syntax error. Am I way off base, or would it be useful if variable length argument lists could be written this way?
> > 
> > If the slurpy arrays could be used anywhere in the list, if becomes impossible to parse the arguments correctly.
> > 
> > In this case, you'll need to separate the value yourself:
> > 
> > def =( \*args )  
> > &nbsp;&nbsp;&nbsp;&nbsp;value = args.pop  
> > &nbsp;&nbsp;&nbsp;&nbsp;cooridinates = args  
> > &nbsp;&nbsp;&nbsp;&nbsp;# ...  
> > end
> > 
> > Hope that helps.
> > 
> > James Edward Gray II
> 
> Thanks! That looks a lot better.
> 
> Daniel

---

<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:** [11 September 2005 19:46 UTC](https://rubytalk.org/t/array/20745/7 "2005-09-11T19:46:32Z")

</div>

Devin Mullins wrote:

> Daniel Schierbeck wrote:
> 
> \> dimensions. If it just had three, I could write:  
> \>  
> \> def = (x, y, z, value); end  
> \>  
> \> But this being an N-dimensional coordinate system, I have to come up  
> \> with something else. The most logical solution to me is this:  
> \>  
> \> def= (\*coordinates, value); end  
> \>  
> \> But this gives a syntax error. Am I way off base, or would it be  
> \> useful if variable length argument lists could be written this way?
> 
> Yes, that would be useful. It has been discussed many-a-time, before,  
> too. 🙂 For now, you'll have to implement your own solution in-method:  
> def = \*args  
> &nbsp;&nbsp;cords, value = args[0..-2],args[-1]  
> &nbsp;&nbsp;#...  
> end
> 
> Personally, I'd also like to see this:
> 
> def = x, y, value  
> &nbsp;&nbsp;#...  
> ordef x, y, z, value  
> &nbsp;&nbsp;#...  
> ordef \*args #gets here only if neither of the first two match  
> &nbsp;&nbsp;#...  
> end
> 
> But none of this exists. So live with it. 🙂
> 
> (I'd say, "Go write an RCR," but I can't imagine that 3 don't exist  
> already.)

I think that you have the right sentiment, but the implemenation isn't  
so good. It would make methods more compicated things to deal with. To  
achieve signiture bases defintoins I think its best to stick with  
multiple definitions:

&nbsp;&nbsp;def = x, y, value  
&nbsp;&nbsp;&nbsp;&nbsp;#...  
&nbsp;&nbsp;end

&nbsp;&nbsp;def = x, y, z, value  
&nbsp;&nbsp;&nbsp;&nbsp;#...  
&nbsp;&nbsp;end

&nbsp;&nbsp;def = \*args #gets here only if neither of the first two match  
&nbsp;&nbsp;&nbsp;&nbsp;#...  
&nbsp;&nbsp;end

I have often though they would be nice too. But how would you  
distingush one from the other, by arity? Eg. #method(:=, 3) ?

T.

---

<div class="post-metadata">

**Author:** ![Devin\_Mullins](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Devin\_Mullins](https://rubytalk.org/u/Devin_Mullins)\
**Post date:** [11 September 2005 20:20 UTC](https://rubytalk.org/t/array/20745/8 "2005-09-11T20:20:21Z")

</div>

Trans wrote:

> I think that you have the right sentiment, but the implemenation isn't  
> so good. It would make methods more compicated things to deal with. To  
> achieve signiture bases defintoins I think its best to stick with  
> multiple definitions:

There are a couple of problems with the typical definition of multiple defs:  
1. Matz has said that he won't implement them. 🙂  
2. What method are you refering to when you call method, remove\_method, etc.? In Ruby, the method signature is its name -- changing that would be a bigger deal.  
3. It doesn't play well with inheritance. Suddenly you've got to think really hard when overriding a method (making sure to remove all implementations with different arity, or whatever).  
4. It doesn't play well with optional args and splats. My way establishes an obvious order of resolution -- if the first one doesn't match, go onto the next one. The typical way doesn't, and is one of the confusing things about method overloading in C++ (by type in that case, but same diff).

My way might squish ordef definitions together a little more (though you're free to use blank lines), but it maintains the idea of a method as an atomic unit -- the ordef thing is just an assistant to make dealing with different arities nice. It would make method definitions as complicated as you make them. If you really have one method doing three different things based on arity, then your central ordef thing should be nothing but a dispatcher to other methods.

The one problem I see with it -- not reproducible by blocks and define\_method. But, in that case, just do it the old fashioned way, as has already been covered on this thread.

The other is that, if it were adopted, the next thing asked for would be ordef's based on ===:

def foo String =\> bar  
ordef foo Regexp =\> bar  
end

def foo "add" =\> action, \*args  
ordef foo "delete" =\> action, \*args  
end

And that would condone some pretty obvious smells. 🙂

Devin

---

<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:** [12 September 2005 03:01 UTC](https://rubytalk.org/t/array/20745/9 "2005-09-12T03:01:32Z")

</div>

Devin Mullins wrote:

> Trans wrote:
> 
> \>I think that you have the right sentiment, but the implemenation isn't  
> \>so good. It would make methods more compicated things to deal with. To  
> \>achieve signiture bases defintoins I think its best to stick with  
> \>multiple definitions:  
> \>  
> \>  
> There are a couple of problems with the typical definition of multiple defs:  
> 1. Matz has said that he won't implement them. 🙂  
> 2. What method are you refering to when you call method, remove\_method,  
> etc.? In Ruby, the method signature is its name -- changing that would  
> be a bigger deal.  
> 3. It doesn't play well with inheritance. Suddenly you've got to think  
> really hard when overriding a method (making sure to remove all  
> implementations with different arity, or whatever).  
> 4. It doesn't play well with optional args and splats. My way  
> establishes an obvious order of resolution -- if the first one doesn't  
> match, go onto the next one. The typical way doesn't, and is one of the  
> confusing things about method overloading in C++ (by type in that case,  
> but same diff).
> 
> My way might squish ordef definitions together a little more (though  
> you're free to use blank lines), but it maintains the idea of a method  
> as an atomic unit -- the ordef thing is just an assistant to make  
> dealing with different arities nice. It would make method definitions as  
> complicated as you make them. If you really have one method doing three  
> different things based on arity, then your central ordef thing should be  
> nothing but a dispatcher to other methods.
> 
> The one problem I see with it -- not reproducible by blocks and  
> define\_method. But, in that case, just do it the old fashioned way, as  
> has already been covered on this thread.

You make some good points. But it occurs to me that having seperated  
defs could in effect work in the same way as what you propose, with the  
excpetion of controlling the order --but that I think should be airty  
dictated anyway. How does the following, for instance, make any sense:

&nbsp;&nbsp;def x(\*a)  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;ordef x(a)  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end

If it works like a case statement, the the last will never be reached.  
Also if this is just a helper, you probaby could do it now with a case  
statement, let me see...

&nbsp;&nbsp;def x(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;case args.size  
&nbsp;&nbsp;&nbsp;&nbsp;when 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;when 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = \*args  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;when 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a,b = \*args  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

Pretty simpe, if not quite as concise. Also I reacall a "real" solution  
to this somewhere --writen by an old school Japanese Ruby Hacker. But I  
can't seem to track it down. Anyone recall?

> The other is that, if it were adopted, the next thing asked for would be  
> ordef's based on ===:
> 
> def foo String =\> bar  
> ordef foo Regexp =\> bar  
> end
> 
> def foo "add" =\> action, \*args  
> ordef foo "delete" =\> action, \*args  
> end
> 
> And that would condone some pretty obvious smells. 🙂

You betchya 🙂

T.

---

<div class="post-metadata">

**Author:** ![Devin\_Mullins](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Devin\_Mullins](https://rubytalk.org/u/Devin_Mullins)\
**Post date:** [12 September 2005 03:26 UTC](https://rubytalk.org/t/array/20745/10 "2005-09-12T03:26:52Z")

</div>

Trans wrote:

> If it works like a case statement, the the last will never be reached.

Yep. Shame on whomever wrote that code! 😛

> Also if this is just a helper, you probaby could do it now with a case  
> statement, let me see...
> 
> \<snip/\>
> 
> Pretty simpe, if not quite as concise.

Yeah, I guess what I'm seeking is a concise way to write that. Maybe it's not deserving of its own construct, though. Can you think of a more general solution that gets half-way there? Maybe a pattern-matching thing akin to the way OCaml does case statements...

def x(\*args)  
&nbsp;&nbsp;case args  
&nbsp;&nbsp;&nbsp;&nbsp;when then ...  
&nbsp;&nbsp;&nbsp;&nbsp;when [a] then ...  
&nbsp;&nbsp;&nbsp;&nbsp;when [a,b] then ...  
&nbsp;&nbsp;&nbsp;&nbsp;else ...  
&nbsp;&nbsp;end  
end

That is totally un-Ruby, of course, so, I dunno.

> Also I reacall a "real" solution  
> to this somewhere --writen by an old school Japanese Ruby Hacker. But I  
> can't seem to track it down. Anyone recall?

'd be interested to hear.

Thanks,  
Devin

---

<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:** [12 September 2005 04:21 UTC](https://rubytalk.org/t/array/20745/11 "2005-09-12T04:21:31Z")

</div>

hmmm....

&nbsp;&nbsp;# holds some nift binding methods like []=  
&nbsp;&nbsp;# for setting local vars, essentially

> **···**
>
> #  
> &nbsp;&nbsp;# eval %{ #{var}=#{val} }, binding  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;require 'nano/binding'
> 
> &nbsp;&nbsp;class Array  
> &nbsp;&nbsp;&nbsp;&nbsp;def to\_p(bind)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;P.new(bind,\*self)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;class P \< Array  
> &nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :bind  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@bind = args.shift if Binding === args.first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.replace(args)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def ===(args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if size == args.size  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index{ |k, i|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args.bind[k] = args[i]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def prms(bind,\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;Params.new(bind,\*args)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def x(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;a,b = nil,nil  
> &nbsp;&nbsp;&nbsp;&nbsp;case args.to\_p(binding)  
> &nbsp;&nbsp;&nbsp;&nbsp;when P[] then p []  
> &nbsp;&nbsp;&nbsp;&nbsp;when P[:a] then p [a]  
> &nbsp;&nbsp;&nbsp;&nbsp;when P[:a,:b] then p [a,b]  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "don't know"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;x  
> &nbsp;&nbsp;x(1)  
> &nbsp;&nbsp;x(1,2)  
> &nbsp;&nbsp;x(1,2,3)

---

<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:** [12 September 2005 04:26 UTC](https://rubytalk.org/t/array/20745/12 "2005-09-12T04:26:32Z")

</div>

Oh, you can ignore/delete this part:

&nbsp;&nbsp;def prms(bind,\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;Params.new(bind,\*args)  
&nbsp;&nbsp;end

T.
