1

I am trying to accept input of two integers, separated by a space: 3 5 and save that into an integer array. I do this 3 times, but I am having trouble converting this from string to integers. Here is my loop:

for i in 1..3
    puts "What is point " + i.to_s + " ?"    # asks for input
    s.push gets.split(" ")
end

Then, I want to have

if s[1][0] - s[0][0] = 0
    blah blah blah
end

The array s currently looks like

------------
| "1"  "2" |
| "3"  "4" |
| "5"  "6" |
------------

I want it to look like

--------
| 1  2 |
| 3  4 |
| 5  6 |
--------

I have tried gets.split(" ").map { |s| s.to_i } and gets.split(" ").collect{|i| i.to_i} but I realize I should just ask someone here.

I have just started learning Ruby, so if you don't mind a short explanation with the solution, i would really appreciate it :)

Thanks!

Note: This is pretty much the opposite of this question, and I did try to use .collect and .map following the loop, which still did not work.

Community
  • 1
  • 1
Paul Hoffer
  • 12,606
  • 6
  • 28
  • 37
  • What *is* being stored in `s`? If you are using an older version of ruby, you should try `<<` instead of `push`, ex: `s << gets.split(' ')`. Your code as it is works as you want it to on my computer. – Adrian Jul 26 '10 at 02:08
  • Currently stored is the array I want, but stored as a strings. I'm using Ruby 1.9.1. ( I also edited original question) – Paul Hoffer Jul 26 '10 at 02:10
  • 1
    I am using 1.9.1 too, and your exact code (with the `gets.split(" ").map { |s| s.to_i }` instead of just `gets.split(" ")` is producing arrays of integers. – Adrian Jul 26 '10 at 02:13
  • I got it working now, thank you. That was very strange. I think before I may have had `map { |i| i.to_i }`, would that have caused it to fail for some reason? I was previously getting `undefined method 'to_i' for ["1", "2"]:Array` – Paul Hoffer Jul 26 '10 at 02:19
  • @phoffer: I don't think so, maybe. – Adrian Jul 26 '10 at 02:21
  • With that error, it was probably because you tacked `map` on after the for loop, instead of after `split`. – Adrian Jul 26 '10 at 02:22

2 Answers2

2

Okay, sorry, I just saw the mistake. I can't believe I missed that.

In your if statement, you are using = instead of ==. = assigns things, and == compares things. A full working program could look like this:

s = []
for i in 1..3
    puts "What is point " + i.to_s + " ?"    # asks for input
    s.push gets.split(" ").map {|x| x.to_i }
end
if s[1][0] - s[0][0] == 0 # notice the '=='.
    puts 'It worked!'
end
Adrian
  • 14,931
  • 9
  • 45
  • 70
  • I did fix that after I asked the question, I realized how stupid of me. I had some `puts` in between which were showing it failing though, so the actual error was separate from that. Thank you for your help though! – Paul Hoffer Jul 26 '10 at 02:23
1

Your answer looks good to me. You might replace s.push with s << (more Rubyish) and there is no need for split's argument. Though I'm a newbie, I think most Rubyiests would write:

s << gets.split.map {|x| x.to_i}

Also, you could replace

puts "What is point " + i.to_s + " ?"

with

puts "What is point #{i} ?"

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
  • What does << mean? I didn't know I could drop the argument on split. And then it makes sense that I can drop the .chomp too – Paul Hoffer Jul 26 '10 at 05:03
  • << is the append operator. You could write, for example, dog = '' << 'ca' << 'nin' << 'e' => "canine" Note if a = 'ca' and b = 'nine', c = a << b => "canine", but also a => "canine", which may not be what you want. << works similarly with arrays: q = [] << 'quick' << 'brown' << 'fox' => ["quick", "brown", "fox"] – Cary Swoveland Jul 26 '10 at 18:58
  • Oh ok. Thanks, that is really good to know! If that isn't what I want, I would just use `+` instead. Good to know these little things. – Paul Hoffer Jul 28 '10 at 00:48