New to Programming: Value of gets for just "Enter"

M

Matt Garriott

Hello,
I am new to programming in general, and I was having a difficult time
figuring out the solution to this problem as well as thinking of good
key words that would help me find the solution online.

My problem is simply that I do not know what the value of a 'gets' that
doesn't contain anything is. I want this program to end if someone just
hits enter for the gets response, and I don't know how to represent that
in the program itself.

puts 'Enter as many words as you please, one per line. Press enter to
quit.'
array = []
input = gets.chomp.downcase
if input != # Not sure what should go here
while input != # Not sure what should go here
array.push input.capitalize
input = gets.chomp.downcase
end
else
end
puts array.sort

If anyone could help me out with this I would be very grateful. I am
sure there is a very simple solution, but I have been looking around
quite a bit, and have been unable to find anything.

Thanks in advance.
 
R

Rick DeNatale

Hello,
I am new to programming in general, and I was having a difficult time
figuring out the solution to this problem as well as thinking of good
key words that would help me find the solution online.

My problem is simply that I do not know what the value of a 'gets' that
doesn't contain anything is. I want this program to end if someone just
hits enter for the gets response, and I don't know how to represent that
in the program itself.

puts 'Enter as many words as you please, one per line. Press enter to
quit.'
array =3D []
input =3D gets.chomp.downcase
if input !=3D =A0# Not sure what should go here
=A0while input !=3D # Not sure what should go here
=A0 =A0array.push input.capitalize
=A0 =A0input =3D gets.chomp.downcase
=A0end
else
end
puts array.sort

If anyone could help me out with this I would be very grateful. I am
sure there is a very simple solution, but I have been looking around
quite a bit, and have been unable to find anything.

It will return an empty string

if input !=3D ""



--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
7

7stud --

Matt said:
Hello,
I am new to programming in general, and I was having a difficult time
figuring out the solution to this problem as well as thinking of good
key words that would help me find the solution online.

My problem is simply that I do not know what the value of a 'gets' that
doesn't contain anything is. I want this program to end if someone just
hits enter for the gets response, and I don't know how to represent that
in the program itself.

print "Enter: "
input = gets
p input

--output:--
Enter:
"\n"
 
7

7stud --

7stud said:
print "Enter: "
input = gets
p input

--output:--
Enter:
"\n"

Whenever you hit Enter/Return on your keyboard, you are adding a
"newline" to the input. A newline is signified by the string "\n".
 
K

Ken Bloom

Whenever you hit Enter/Return on your keyboard, you are adding a
"newline" to the input. A newline is signified by the string "\n".

And then the chomp function which he calls on the string removes the \n
from the end, making it an empty string.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top