Help with Arrays

I have the following code:

words = []

while gets.chomp != ''
  words.push gets.chomp
end

puts words.sort

Its supposed to capture from the user, a bunch of words, until just
enter is pressed, and put them in an array. When I run the code, it
only enters every other word i type into the array. I cannot figure it
out! I know this is simple for you all, so have pity on me! Im super
new to ruby and programming in general.

Thanks in advance.

J

···

--
Posted via http://www.ruby-forum.com/.

Jonathan --- wrote:

I have the following code:

words =

while gets.chomp != ''
  words.push gets.chomp
end

Jonathan,

ruby reads another line each time you call gets or gets.chomp (or gets.anything). You need to cache the result of the call to gets(). I would write:

while line = gets.chomp && line != ""
   words.push line
end

Dan

Dan Zwell wrote:

Jonathan --- wrote:

I have the following code:

words =

while gets.chomp != ''
  words.push gets.chomp
end

Jonathan,

ruby reads another line each time you call gets or gets.chomp (or
gets.anything). You need to cache the result of the call to gets(). I
would write:

while line = gets.chomp && line != ""
   words.push line
end

Dan

Hi Dan,

thanks very much for the response. The program does not seem to get to
end when just the enter is pressed...
Im doing my best to understand...

Sorry to be so thick!

Thanks!

···

--
Posted via http://www.ruby-forum.com/\.

Jonathan Carbon wrote:

Hi Dan,

thanks very much for the response. The program does not seem to get to end when just the enter is pressed...
Im doing my best to understand...

Sorry to be so thick!

Not at all, the thickness is mine...

Thanks!

Giving advice without testing it is a bad habit of mine. I wasn't paying attention to Ruby's precedence. Apparently,
     while line = gets.chomp && line != ""
needs some parentheses:
     while (line = gets.chomp) && line != ""

However, there's another small gotcha. When you write a loop
while line = gets.chomp; something; end
users will often end the loop by hitting control-d, which means "end of file" (on unix/linux, at least). This causes gets() to return nil, and in that case, line.chomp throws an error (nil lacks the method chomp()).

This does what you want and also handles the control-d case:
while line=gets and line.chomp != ""
Note that the chomp() is not cached in this case--in the body of the loop, you need to call chomp() when you refer to the line, or just call "line.chomp!". You could probably be more efficient, but often the Ruby way is either the most easily readable thing, or the most elegant.

Dan

Dan,

Many thanks for the extended response. Its much appreciated.

J

···

--
Posted via http://www.ruby-forum.com/.

I'd rather do this because the test is much easier:

words =

while ( line = gets )
  words << line.chomp
end

However, there is a much shorter solution available:

puts ARGF.readlines.sort

Or even using "sort" directly (on a Unix like system):

$ sort <<XXX

foo
bar
baz
XXX

bar
baz
foo

Kind regards

robert

The original poster wants the loop to end when the user presses
<Enter>
without entering any text. How about this?

words =
while line = gets and line.chomp! != ""
  words << line
end

···

On Aug 17, 3:49 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:

I'd rather do this because the test is much easier:

words =

while ( line = gets )
  words << line.chomp
end

# From: Robert Klemme <shortcut...@googlemail.com>
# > I'd rather do this because the test is much easier:
# > words = []
# > while ( line = gets )
# > words << line.chomp
# > end

···

From: William James [mailto:w_a_x_man@yahoo.com]
#
# The original poster wants the loop to end when the user presses
# <Enter> without entering any text. How about this?
# words = []
# while line = gets and line.chomp! != ""
# words << line
# end

in (my) practice, i usually go the simple way (like robert's), then refine it as needed by just adding the lines.

words = []
while line=gets # break on eof
   break if line.strip!.empty? # break on empty lines including spaces
   next if line != /\w/ # special chars not allowed

   words << line # passed
end

sometimes, (as a nuby,) putting all conditions in one line may be dangerous. it is dificult to test, difficult to comment, and one may miss edge cases...
i know it's not clever, but i value safety first, and it's cleaner and clearer to me.. but ymmv.. i haven't reach the mile yet :slight_smile:

kind regards -botp

Might as well simply evaluate the assignment expression directly:

words =
while (line = gets.chomp) != ''
  words << line.chomp
end
puts words.sort

While that fulfills the original spec, I agree with Dan's comment
regarding <ctrl>-D or EOF. It would be nice to handle that to allow
piping a file of words to the program. Before I noticed Robert's post,
I came up with the following:

puts ARGF.inject() {|memo,obj| memo << obj}.sort

although fun, it's way too verbose. I ended up shortening Robert's to
this:

puts ARGF.sort

If you don't want to create a .rb file, just:

ruby -e 'puts ARGF.sort'

:slight_smile:

···

On Aug 17, 2:04 pm, William James <w_a_x_...@yahoo.com> wrote:

The original poster wants the loop to end when the user presses
<Enter>
without entering any text. How about this?

words =
while line = gets and line.chomp! != ""
  words << line
end