[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Adding arrays

Erik Boling

7/25/2007 10:17:00 PM

I was wondering, if it is possible to add two different arrays together,
but do it line by line. For example: Array_1 has 1, 2 and 3, and
Array_2 has 1, 2, and also 3, then your answer would come out 2, 4, and
6.
*on a side note* i just started ruby (programming for that matter) a few
weeks ago
when my cousimn showed me so i was wondering if there are any good
tutorials/books out there.
--
Posted via http://www.ruby-....

13 Answers

Tim Hunter

7/25/2007 10:25:00 PM

0

Erik Boling wrote:
> *on a side note* i just started ruby (programming for that matter) a few
> weeks ago
> when my cousimn showed me so i was wondering if there are any good
> tutorials/books out there.
>
http://www.rub...
http://www.ruby-lang.org/en/docu...

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Erik Boling

7/25/2007 10:34:00 PM

0

Tim Hunter wrote:
> http://www.rub...
> http://www.ruby-lang.org/en/docu...
Great, I bookmarked them, Im wondering, what is the 1st link all about?
is that just everything ruby can do?
--
Posted via http://www.ruby-....

Tim Hunter

7/25/2007 10:40:00 PM

0

Erik Boling wrote:
> Tim Hunter wrote:
>
>> http://www.rub...
>> http://www.ruby-lang.org/en/docu...
>>
> Great, I bookmarked them, Im wondering, what is the 1st link all about?
> is that just everything ruby can do?
>
Why not just look at the site? It describes itself.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Erik Boling

7/25/2007 10:47:00 PM

0

Tim Hunter wrote:
> Erik Boling wrote:
>> Tim Hunter wrote:
>>
>>> http://www.rub...
>>> http://www.ruby-lang.org/en/docu...
>>>
>> Great, I bookmarked them, Im wondering, what is the 1st link all about?
>> is that just everything ruby can do?
>>
> Why not just look at the site? It describes itself.

ok, thanks for the help so far :)
--
Posted via http://www.ruby-....

Phrogz

7/25/2007 11:02:00 PM

0

On Jul 25, 4:17 pm, Erik Boling <schmod...@yahoo.com> wrote:
> I was wondering, if it is possible to add two different arrays together,
> but do it line by line. For example: Array_1 has 1, 2 and 3, and
> Array_2 has 1, 2, and also 3, then your answer would come out 2, 4, and
> 6.

C:\>irb

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]

irb(main):002:0> b = [4,5,6]
=> [4, 5, 6]

irb(main):003:0> a.zip(b)
=> [[1, 4], [2, 5], [3, 6]]

irb(main):004:0> a.zip(b).map{ |pair| pair[0] + pair[1] }
=> [5, 7, 9]

irb(main):006:0> module Enumerable; def sum; inject(0){ |s,v| s+v }
end; end
=> nil

irb(main):007:0> a.zip(b).map{ |pair| pair.sum }
=> [5, 7, 9]

Erik Boling

7/26/2007 12:26:00 AM

0

Oh Thanks so much, but i dont have the irb main in C:? and its not in my
ruby folder.. so im DL'ing again. I was wondering can somoe one explian
the 3rd, 4th and 5th lines on gavins response

--
Posted via http://www.ruby-....

Erik Boling

7/26/2007 12:42:00 AM

0

Erik Boling wrote:
> Oh Thanks so much, but i dont have the irb main in C:? and its not in my
> ruby folder.. so im DL'ing again. I was wondering can somoe one explian
> the 3rd, 4th and 5th lines on gavins response

Well, nvm about the irb main, the one off of this program *hackety hack*
works fine.
--
Posted via http://www.ruby-....

Todd Benson

7/26/2007 4:11:00 AM

0

On 7/25/07, Erik Boling <schmode93@yahoo.com> wrote:
> Oh Thanks so much, but i dont have the irb main in C:? and its not in my
> ruby folder.. so im DL'ing again. I was wondering can somoe one explian
> the 3rd, 4th and 5th lines on gavins response

irb.bat (along with your other ruby-related executables) should be in
the bin directory inside your ruby directory. For example, mine is
c:\ruby\bin\irb. After you find where it is, you should set your
environment PATH variable if it isn't set correctly already.
Right-click on My Computer, select Properties. Select Advanced tab,
Environment Variables button. Under System Variables, look for your
ruby\bin directory for Path. If it isn't there or is wrong, add it to
the end with a preceding semicolon (;).

third expression: think of the zip function as stacking your arrays,
one atop the other, and making arrays out of the vertical columns; you
now have a main array with arrays (the columns) as its elements

fourth expression: the map function takes each element of an
Enumerable object (of which Array is one type) and does something with
it; in this case, adding the two parts of the element

fifth expression: he extends the behavior of an Enumerable object with
a new method called sum using an existing method inject; inject is an
accumulator function, going through each element and whatever is
evaluated gets injected back into the first parameter (in this case,
s) for the next iteration

The fifth one would be confusing to people new to programming, but its
there so that you can sum over more than just two arrays (which is
what he he was doing in the fourth expression). It is a common way to
sum in Ruby.

You can read about how zip, map, and inject work at http://www.ruby-do...

hth,
Todd

Erik Boling

7/26/2007 4:26:00 AM

0

Great explination, and i did get the irb, thanks a ton todd :).
--
Posted via http://www.ruby-....

Todd Benson

7/26/2007 4:47:00 AM

0

On 7/25/07, Erik Boling <schmode93@yahoo.com> wrote:
> Great explination, and i did get the irb, thanks a ton todd :).

No prob. If you really are just starting, don't get too frustrated
with "advanced" methods like zip and inject. They both are powerful,
but you can get by without them to start. For list-type objects
(Enumerable, Hash, Array, etc.), Ruby provides a swiss army knife of
methods to slice and dice them. Get to know your enumerables! Get
intimate with the String class, too!

cheers,
Todd