How do you put the "gets" input into an array?

Ruby noob, studying the ropes. I am currently experiencing this tutorial and working on this exercise:

Let me write a program that asks us to enter 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.

Now I ignore the part in alphabetical order.

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\ what you told me: ' +array.to_s

I fixed it in a few hours. To prevent your laptop from breaking due to frustration, I take a break. Can anyone with more experience and possibly more patience point out my mistakes?

+3
source share
7 answers

, , gets, , . :

array = [gets]
yn = gets.chomp

. - (, , . ) , .

, array = [gets] , ( ). . while << :

array = Array.new
...
while yn != "no"
  ...
  array << gets.chomp
  yn = array.last
  ...
end
+6

-, , , - .

, gets , gets.

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

, yn , .

, , ,

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

, , , array yn , .

, Ruby, . Ruby?

+2
while yn != "no"
  array << yn
  print "What else? "
  yn = gets.chomp
end

"< yn . ( , print, , , . )

+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!!!
+1

. ( , ):

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
+1

" :

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.
0

:

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

All Articles