Nil or enter with no input

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

···

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

Sorry I may have gotten it, check this out.

# Building and sorting an array.

array = []
puts 'Enter a word'
word = gets.chomp

while word != ''
  array.push word
  puts 'Enter a word'
  word = gets.chomp
end

puts array.sort

···

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

You might want to look into String#empty?

···

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

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.

--
<https://github.com/stomar/&gt;

Phil H. wrote:

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

Tom Reilly

That alone is not sufficient. You need to at least throw String#chomp in
the mix:

array =
word = nil

do
  puts 'Enter a word'
  word = gets.chomp # cut off line endings
  array.push word unless word.empty?
end until word.empty?

puts array.sort

Another approach would verify that the string does indeed contain a word:

...
do
  puts 'Enter a word'
  word = gets.chomp # cut off line endings
  array.push word unless /\A\w+\z/ =~ word
end until word.empty?

We can also avoid the duplicate check:

array =
word = nil

loop do
  puts 'Enter a word'
  word = gets.chomp # cut off line endings
  break if word.empty?
  array.push word
end

puts array.sort

Kind regards

robert

···

On Fri, Mar 29, 2013 at 5:42 PM, Joel Pearson <lists@ruby-forum.com> wrote:

You might want to look into String#empty?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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
:slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.

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
:slight_smile:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/