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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
20ac61b323d46d17ad3f05aaf4aaddef?d=identicon&s=25 Matt Garriott (spiff4)
on 2009-06-07 01:50
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.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-06-07 03:26
(Received via mailing list)
On Sat, Jun 6, 2009 at 7:50 PM, Matt Garriott<matt.garriott@gmail.com>
wrote:
> puts 'Enter as many words as you please, one per line. Press enter to
> 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 != ""



--
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-06-08 07:47
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"
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-06-08 07:59
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".
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-06-08 17:09
(Received via mailing list)
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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-06-08 18:14
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?
21dd018d2954c5c1f9f2fe00a18f6d75?d=identicon&s=25 Gabriel Medina (rha7dotcom)
on 2009-06-09 04:31
(Received via mailing list)
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 != ""
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2009-06-09 04:41
(Received via mailing list)
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
=> ""
21dd018d2954c5c1f9f2fe00a18f6d75?d=identicon&s=25 Gabriel Medina (rha7dotcom)
on 2009-06-09 20:48
(Received via mailing list)
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
This topic is locked and can not be replied to.