Adding arrays

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.

Erik B. 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.ruby-lang.org/en/documentation/

Erik B. wrote:

Tim H. wrote:

http://www.ruby-doc.org/
http://www.ruby-lang.org/en/documentation/

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.

Tim H. wrote:

http://www.ruby-doc.org/
http://www.ruby-lang.org/en/documentation/
Great, I bookmarked them, Im wondering, what is the 1st link all about?
is that just everything ruby can do?

Tim H. wrote:

Erik B. wrote:

Tim H. wrote:

http://www.ruby-doc.org/
http://www.ruby-lang.org/en/documentation/

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 :slight_smile:

On Jul 25, 4:17 pm, Erik B. [email protected] 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 B. 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.

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

On 7/25/07, Erik B. [email protected] 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-doc.org/core

hth,
Todd

On 7/25/07, Erik B. [email protected] 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

Todd B. wrote:

On 7/25/07, Erik B. [email protected] 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

humm ok so i tried out my program but at the very end i get an error…
irb(main):001:0> def ask(question)
irb(main):002:1> puts “#{question}:\n”
irb(main):003:1> gets.split
irb(main):004:1> end
=> nil
irb(main):005:0> Var_1 = ask(“Enter 1st Vendors prices”)
Enter 1st Vendors prices:
14.50 16.50 14
=> [“14.50”, “16.50”, “14”]
irb(main):006:0> Var_2 = ask(“Enter 2nd vendors prices”)
Enter 2nd vendors prices:
13 17.50 14
=> [“13”, “17.50”, “14”]
irb(main):007:0> Var_1.zip(Var_2)
=> [[“14.50”, “13”], [“16.50”, “17.50”], [“14”, “14”]]
irb(main):008:0> Var_1.zip(Var_2).map{ |pair| pair[0] - pair[1] }
NoMethodError: undefined method -' for "14.50":String from (irb):8 from (irb):8:in map’
from (irb):8
from :0
irb(main):009:0>

could it because im using floats, and not intergers?

Great explination, and i did get the irb, thanks a ton todd :).

On 2007-07-27, Erik B. [email protected] wrote:

NoMethodError: undefined method `-' for "14.50":String

could it because im using floats, and not intergers?

You’re not using either. You’re taking straight from standard input, so
you’re reading in Strings, like the error message says. To convert these
values to floats, use String#to_f.

humm ok so i tried out my program but at the very end i get an error…
irb(main):001:0> def ask(question)
irb(main):002:1> puts “#{question}:\n”
irb(main):003:1> gets.split
irb(main):004:1> end
=> nil
irb(main):005:0> Var_1 = ask(“Enter 1st Vendors prices”)
Enter 1st Vendors prices:
14.50 16.50 14
=> [“14.50”, “16.50”, “14”]
irb(main):006:0> Var_2 = ask(“Enter 2nd vendors prices”)

gahhh! sorry posted this on wrong forum, please look at re- beginner q.
sorry guys, dont mean to spam the forums!