How can I tell if someone has enter nothing in a gets prompt, they only
press enter. I though maybe it was nil, but that doesn't seem to work. I
though this was going to work nicely, but doesn't.
# Building and sorting an array.
array = []
word = 'foo'
while word != nil
puts 'Enter a word'
word = gets
array.push word
end
The string will be "\n" (a string that contains only a newline):
if gets == "\n"
or
if gets.chomp.empty?
will work.
···
Am 29.03.2013 16:55, schrieb Phil H.:
How can I tell if someone has enter nothing in a gets prompt, they only
press enter. I though maybe it was nil, but that doesn't seem to work. I
though this was going to work nicely, but doesn't.
How can I tell if someone has enter nothing in a gets prompt, they only
press enter. I though maybe it was nil, but that doesn't seem to work. I
though this was going to work nicely, but doesn't.
# Building and sorting an array.
array =
word = 'foo'
while word != nil
puts 'Enter a word'
word = gets
array.push word
end
puts array.sort
Using enter even with no other character will return a \r\n or \n depending upon your operating system
Right you are. I am sorry, I didn't notice Phil added it in his second
post.
Cheers
robert
···
On Sat, Mar 30, 2013 at 9:16 AM, Joel Pearson <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1103733:
>> You might want to look into String#empty?
> That alone is not sufficient. You need to at least throw String#chomp
> in the mix:
I he was already using chomp, so I didn't see a point in mentioning it