[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Matt Garriott

6/6/2009 11:51:00 PM

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.
--
Posted via http://www.ruby-....

8 Answers

Rick DeNatale

6/7/2009 1:26:00 AM

0

On Sat, Jun 6, 2009 at 7:50 PM, Matt Garriott<matt.garriott@gmail.com> wrot=
e:
> 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.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

7stud --

6/8/2009 5:47:00 AM

0

Matt Garriott wrote:
> 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"

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

7stud --

6/8/2009 6:00:00 AM

0

7stud -- wrote:
>
> 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".

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

Ken Bloom

6/8/2009 3:08:00 PM

0

On Mon, 08 Jun 2009 14:59:31 +0900, 7stud -- wrote:

> 7stud -- wrote:
>>
>> 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".

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



--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...


7stud --

6/8/2009 4:15:00 PM

0

Ken Bloom wrote:
> On Mon, 08 Jun 2009 14:59:31 +0900, 7stud -- wrote:
>
>> 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.

Really?
--
Posted via http://www.ruby-....

Rha7

6/9/2009 2:28:00 AM

0

7stud -- wrote:
> Ken Bloom wrote:
>> On Mon, 08 Jun 2009 14:59:31 +0900, 7stud -- wrote:
>>
>>> 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.
>
> Really?

if input.chomp.strip != ""

botp

6/9/2009 2:38:00 AM

0

On Tue, Jun 9, 2009 at 10:30 AM, Rha7<rha7.com@gmail.com> wrote:
> if input.chomp.strip != ""

irb(main):001:0> "\n".strip
=> ""
irb(main):002:0> " \n ".strip
=> ""

Rha7

6/9/2009 6:41:00 PM

0

botp wrote:
> On Tue, Jun 9, 2009 at 10:30 AM, Rha7<rha7.com@gmail.com> wrote:
>> if input.chomp.strip != ""
>
> irb(main):001:0> "\n".strip
> => ""
> irb(main):002:0> " \n ".strip
> => ""
>
rha7.illuminated.chomp