[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: symbol tricks

Gavin Kistner

9/8/2006 4:34:00 PM

From: Jason Nordwick [mailto:jason@adapt.com]
> Plus for arrays and strings is concatenation. I don't like it
> either, b/c array plus should be addition of corresponding
> elements, but that is the way things are.

That's seems oddly specific to me, but if you don't like it, change it:

a = [ 1, 2, 3 ]
b = [ 4, 5, 6 ]
p a+b
#=> [1, 2, 3, 4, 5, 6]

class Array
def +( other )
self.zip( other ).map{ |e| e[0]+e[1] }
end
end

p a+b
#=> [5, 7, 9]

1 Answer

Jason Nordwick

9/8/2006 5:15:00 PM

0



Gavin Kistner wrote:
> From: Jason Nordwick [mailto:jason@adapt.com]
>> Plus for arrays and strings is concatenation. I don't like it
>> either, b/c array plus should be addition of corresponding
>> elements, but that is the way things are.
>
> That's seems oddly specific to me, but if you don't like it, change it:
>

Wouldn't that break pretty much everything else that used + for concatenation?

-j

> a = [ 1, 2, 3 ]
> b = [ 4, 5, 6 ]
> p a+b
> #=> [1, 2, 3, 4, 5, 6]
>
> class Array
> def +( other )
> self.zip( other ).map{ |e| e[0]+e[1] }
> end
> end
>

> p a+b
> #=> [5, 7, 9]
>