# Entering nil string value through gets

**URL:** <https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227>\
**Category:** Ruby\
**Created:** [November 25, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227 "2007-11-25T18:18:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 25, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227/1 "2007-11-25T18:18:21Z")

</div>

when i enter nil for gets, it is not comparing with another string  
having nil value.

tried gets.chomp which also gives the same problem.

any help?

democrats = [“Carter”, “Clinton”]  
puts ‘x1’  
republicans = [“Ford”, “Reagan”, “Bush1”, “Bush2”]  
puts ‘x2’

puts “Enter nil or democrats or republicans or any for party =”

#party = gets # without chomp all comparison fails.  
#puts party

#party = gets.chomp # nil fails  
#puts party

#party1 = gets #  
#party = party1.chomp # nil fails  
#puts party, party1

#party.chomp = gets # err. undefined var or method party  
#puts party

#party1 = gets #  
#party.chomp = party1 # err. undefined var or method party  
#puts party, party1

if party != nil  
democrats.each { |i| print i, " "} if party == “democrats”  
puts ‘x5’  
republicans.each { |i| print i, " "} if party == “republicans”  
puts ‘x6’  
print “All presidents since 1976 were either Democrats or  
Republicans\n”  
if (party != “democrats” && party != “republicans”)  
end

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 25, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227/2 "2007-11-25T19:07:09Z")

</div>

On Nov 25, 2007 6:18 PM, Nathan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
gets gives you a string or nil at EOF  
I would not rely on EOF (D) for interactive input but the choice  
is yours of course, two approaches seem reasonable at first sight  
puts “Enter democrats or republicans or any for party = or just ”

unless party.empty?  
case party  
when “democrats”…  
puts( democrats.join(" ") \<\< ‘x5’ )

end

or

puts “Enter democrats or republicans or any for party = or just D”

case party  
when “democrats”  
…  
when nil  
whatever  
end

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 25, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227/3 "2007-11-25T21:12:24Z")

</div>

Nathan V. wrote:

> when i enter nil for gets, it is not comparing with another string  
> having nil value.

How did you enter nil for gets?

> tried gets.chomp which also gives the same problem.
> 
> any help?
> 
> #party = gets.chomp # nil fails
> 
> if party != nil  
> democrats.each { |i| print i, " "} if party == “democrats”  
> puts ‘x5’  
> republicans.each { |i| print i, " "} if party == “republicans”  
> puts ‘x6’  
> print “All presidents since 1976 were either Democrats or  
> Republicans\n”  
> if (party != “democrats” && party != “republicans”)  
> end

input = “nil\n”  
input = input.chomp  
puts input.class #String

some\_var = nil  
puts some\_var.class #NilClass

if input == some\_var  
puts “The user entered the ruby nil object.”  
else  
puts “The user didn’t enter the ruby nil object.”  
end

–output:–  
The user didn’t enter the ruby nil object.

some\_var = ‘nil’

if input == some\_var  
puts “The user entered a string containing the characters n, i, and  
l.”  
else  
puts "The user didn’t enter a string containing the charcters n, i,  
and l."end

–output:–  
The user entered a string containing the characters n, i, and l.

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 25, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227/4 "2007-11-25T21:28:34Z")

</div>

Robert D. wrote:

> I would not rely on EOF (D) for interactive input

One data point: using Ctrl-D to enter EOF in interactive input works on  
mac os x.

## gets(…) =\> string or nil

```
 ...
Returns +nil+ at end of file.

```

party = gets

if party != nil  
party = party.chomp

if party == ‘democrats’  
puts ‘dems’  
elsif party == ‘republicans’  
puts ‘rep’  
end

end

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 26, 2007, 2:06am UTC](https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227/5 "2007-11-26T02:06:39Z")

</div>

Nathan V. wrote:  
thanks to Robert and 7stud.

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [November 25, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/entering-nil-string-value-through-gets/121227/6 "2007-11-25T21:42:49Z")

</div>

Nathan V. wrote:

> democrats.each { |i| print i, " "} if party == “democrats”  
> puts ‘x5’  
> republicans.each { |i| print i, " "} if party == “republicans”  
> puts ‘x6’  
> print “All presidents since 1976 were either Democrats or  
> Republicans\n”  
> if (party != “democrats” && party != “republicans”)  
> end

By the way, constructs like the above require that ruby evaluate all  
three of the conditionals, where with a construct like:

if party == ‘democrats’  
puts ‘dems’  
elsif party == ‘republicans’  
puts ‘rep’  
else  
puts “All presidents…”  
end

as soon as one if-conditional is found to be true, the others below it  
are not evaluated. Also notice that no complex conditional has to be  
used for the else clause.

The ruby syntax where an if conditional comes at the end of a statement  
is a particularly horrid one. I suggest you avoid it completely.
