4

Ruby noob here learning the ropes. I'm currently going through this tutorial and am working on this exercise:

Let's write a program which asks us to type in as many words as we want (one word per line, continuing until we just press Enter on an empty line), and which then repeats the words back to us in alphabetical order.

I'm ignoring the alphabetical order part, for now.

Here is my code:

puts 'Hi, do you need something sorted?'
yn = gets.chomp
while yn != 'no'
  puts 'What else?'
  array = [gets]
  yn = gets.chomp
end
puts 'Here\'s what you told me: ' +array.to_s

I've tweaked this for a few hours. To prevent my laptop from breaking due to an act of frustration I'm taking a break. Can anybody with more experience, and possibly more patience, point out my errors?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
RubyStn
  • 41
  • 1
  • 1
  • 2

7 Answers7

7

Keep in mind that every time you gets is a method that asks the user for input. On your lines:

array = [gets]
yn = gets.chomp

You are actually asking for input twice. Instead, store the user input somewhere (such as the array, see below) and get the stored value rather than asking the user twice.

Further, array = [gets] replaces the existing array with an array containing one element (the user input). You are never building up user input into the array. Instead, initialize the array before the while loop and use << in the loop:

array = Array.new
...
while yn != "no"
  ...
  array << gets.chomp
  yn = array.last
  ...
end
Martin Gordon
  • 36,329
  • 7
  • 58
  • 54
  • 1
    The only issue with this is the last item of the array will always be "no". Might want to array.pop if you don't want that "no" in there. – Charles Caldwell Mar 08 '11 at 16:31
2

I was having the same problem. Here is where I ended up (I think it meets all the specifications from the question):

puts 'Type in as many words as you\'d like. When you\'re finished, press enter on an empty line'
array = []
input = ' '
while input != ''
  input = gets.chomp
  array.push input
end

puts
puts array.sort
2

If you're having difficulty with something, the first thing you should do is try something simpler.

Rather than doing gets and looping, just try doing a simple gets.

puts 'Hi, do you need something sorted?'
yn = gets.chomp

Then I'd see if yn was what I expected.

The next thing I'd do is, rather than doing a loop many times, just try it once

puts 'Hi, do you need something sorted?'
yn = gets.chomp
if yn != 'no'
  puts 'What else?'
  array = [gets]
  yn = gets.chomp
  STDERR.puts "array is #{array.inspect}"
  STDERR.puts "yn is #{yn.inspect}"
end

Then you'd hopefully realize that array and yn are both getting input, which wouldn't make sense.

For more hints on how to debug Ruby code, see How do I debug Ruby scripts?

Community
  • 1
  • 1
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
1
while yn != "no"
  array << yn
  print "What else? "
  yn = gets.chomp
end

The "<<" appends yn to your array. (The only reason I used print is because it puts the cursor right next to the question mark instead of on the next line. No other reason)

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
1
#encoding:utf-8

x = Array.new
puts "enter something:".capitalize
y = ''
while y !=#nill
  y = gets.chomp
  x.push y
end
x.delete ('')
x.compact
puts "You entered: " + x.sort.to_s
puts "Objects in array: " + x.size.to_s
 #made by ~Pick@chu!!!
0

Another way to read ‘Arrays’ from the console could be:

1: print “enter the values: ”
2: a = gets.chomp # input: “tom mark rosiel suresh albert”
3: array = a.split(‘ ‘) # .split() method return an array
4: p array # ["tom, "mark, "rosiel", "suresh", "albert"]

now, lets say you want an array of integers, all you have to do is:

# input “1 2 3 4 5″
3: array = a.split(‘ ‘).map{ |value| value.to_i }
4: p array # [1, 2, 3, 4, 5]

the clue here is to use a standard separator in order to use the .split() function.
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alfonso
  • 758
  • 5
  • 8
0

here is how I've done this program:

array = [ ]
input = gets.chomp
while
  input != ''
  array.push input
  input = gets.chomp  
end
puts array
puts
puts array.sort