# Nil or enter with no input

**URL:** <https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822>\
**Category:** Ruby\
**Created:** [March 29, 2013, 4:55pm UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822 "2013-03-29T16:55:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 29, 2013, 4:55pm UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/1 "2013-03-29T16:55:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 29, 2013, 5:01pm UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/2 "2013-03-29T17:01:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 29, 2013, 5:42pm UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/3 "2013-03-29T17:42:06Z")

</div>

You might want to look into String#empty?

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 29, 2013, 11:36pm UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/4 "2013-03-29T23:36:33Z")

</div>

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.

The string will be “\n” (a string that contains only a newline):

if gets == “\n”

or

if gets.chomp.empty?

will work.

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 30, 2013, 1:26am UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/5 "2013-03-30T01:26:23Z")

</div>

Phil H. wrote:

> 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 R.

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 29, 2013, 11:29pm UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/6 "2013-03-29T23:29:32Z")

</div>

On Fri, Mar 29, 2013 at 5:42 PM, Joel P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You might want to look into String#empty?

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

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 30, 2013, 9:16am UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/7 "2013-03-30T09:16:23Z")

</div>

Robert K. 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  
🙂

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 30, 2013, 10:56am UTC](https://www.ruby-forum.com/t/nil-or-enter-with-no-input/227822/8 "2013-03-30T10:56:28Z")

</div>

On Sat, Mar 30, 2013 at 9:16 AM, Joel P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Robert K. 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  
> 🙂

Right you are. I am sorry, I didn’t notice Phil added it in his second  
post.

Cheers

robert
