Entering nil string value through gets

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

On Nov 25, 2007 6:18 PM, Nathan V. [email protected] 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

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.

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

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

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.