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.
*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.
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.
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
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.
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.
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
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
···
On 7/25/07, Erik Boling <schmode93@yahoo.com> wrote:
Great explination, and i did get the irb, thanks a ton todd :).
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
irb(main):009:0>
could it because im using floats, and not intergers?
···
On 7/25/07, Erik Boling <schmode93@yahoo.com> wrote:
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!
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.
···
On 2007-07-27, Erik Boling <schmode93@yahoo.com> wrote:
<snip>
NoMethodError: undefined method `-' for "14.50":String
</snip>
could it because im using floats, and not intergers?