Todd Benson
7/27/2007 4:10:00 AM
On 7/26/07, Erik Boling <schmode93@yahoo.com> wrote:
> Gabe Boyer wrote:
> >> Although I'd have to point out that 'ask' is an awful name for a method.
> >
> > I'd say that depends entirely on context. In a context like Hackety
> > Hack, I think it's a perfectly good name.
> >
> > -Gabriel Boyer
>
> 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"]
Your elements are Strings! That's what you get back from the gets
method. Let's change them to Floats. But first, let's not use
starting capital letters for our variables...
irb> var_1, var_2 = Var_1, Var_2
=> [["14.50", "16.40", 14"], ["13","17.50", "14"]]
irb> [var_1, var_2].each { |array| array.map! { |elem| elem.to_f } }
=> [[14.5, 16.5, 14.0], [13.0, 17.5, 14.0]]
> irb(main):007:0> Var_1.zip(Var_2)
You don't need this ^^^^^^^^ line
irb> difference = var_1.zip(var_2).map { |pair| pair[0] - pair[1] }
=> [1.5, -1.0, 0.0]
Todd