Help with Arrays...

J

Jonathan ---

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
 
D

Dan Zwell

Jonathan said:
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
 
J

Jonathan Carbon

Dan said:
Jonathan said:
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!
 
D

Dan Zwell

Jonathan said:
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...

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
 
R

Robert Klemme

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
 
W

William James

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

words = []

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

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
 
P

Peña, Botp

From: William James [mailto:[email protected]]=20
# From: Robert Klemme <[email protected]>
# > I'd rather do this because the test is much easier:
# > words =3D []
# > while ( line =3D gets )
# > words << line.chomp
# > end
#=20
# The original poster wants the loop to end when the user presses
# <Enter> without entering any text. How about this?
# words =3D []
# while line =3D gets and line.chomp! !=3D ""
# 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 =3D []
while line=3Dgets # break on eof
break if line.strip!.empty? # break on empty lines including spaces
next if line !=3D /\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...=20
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 :)

kind regards -botp
 
B

Brian Adkins

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

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'

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top