chomp - works in irb, not in program

P

Peter Vanderhaden

When I try to use chomp to get user input, I get this error:

private method `chomp' called for nil:NilClass (NoMethodError)

from d:/scripts/ruby/fix1.rb:82:in `run'
from d:/scripts/ruby/fix1.rb:128

When I use it in irb, however it works. Anyone have any ideas what I
might be missing here?
Thanks....
 
S

Sebastian Hungerecker

Peter said:
When I try to use chomp to get user input

You can't get user input with chomp. The only thing you get from chomp is a
version of the string you called it on with trailing character removed.

private method `chomp' called for nil:NilClass (NoMethodError)

Yes, there is no chomp method for nil. You need to call chomp on a String. If
you thought that you were calling it on a String, you were thinking wrong.
You should look into that.
 
D

Douglas Greenshields

How are you using it in the program? Are you using gets.chomp? Can you
show a code sample?
 
P

Peter Vanderhaden

Douglas,
Here's the method I'm using:

def checkChange
puts "Do you want to change something else (y/N): "
changeIt = gets.chomp
if changeIt =~ /^[yY]/
puts "Enter string to change: "
before = gets.chomp
puts "Enter new string: "
after = gets.chomp
puts "Global change? (N/y): "
global = gets.chomp
end
end # End checkChange method.
 
P

Peter Vanderhaden

Douglas:
I forgot to mention that the puts command outputs the prompt to the
screen, but the program never pauses to give the user a chance to enter
anything for an answer. I'm assuming that's why the .chomp part doesn't
work. The first thing I need to do is figure out why gets isn't causing
the program to pause to accept the user input.
Thanks,
PV


Peter said:
Douglas,
Here's the method I'm using:

def checkChange
puts "Do you want to change something else (y/N): "
changeIt = gets.chomp
if changeIt =~ /^[yY]/
puts "Enter string to change: "
before = gets.chomp
puts "Enter new string: "
after = gets.chomp
puts "Global change? (N/y): "
global = gets.chomp
end
end # End checkChange method.




Douglas said:
How are you using it in the program? Are you using gets.chomp? Can you
show a code sample?
 
S

Sebastian Hungerecker

Peter said:
Douglas:
I forgot to mention that the puts command outputs the prompt to the
screen, but the program never pauses to give the user a chance to enter
anything for an answer.

Are you by any chance running the script with any command line arguments?
That would cause Kernel#gets to read from disk instead of from stdin which
might explain the behaviour you're experiencing.


HTH,
Sebastian
 
D

Douglas Greenshields

Yes, as you imply, it's an issue with gets, not with chomp. The method
you posted looks pretty textbook - if gets can work, then chomp will
work. For some reason it looks like gets is not pausing the script
application and just returning nil.
 
S

Sebastian Hungerecker

Douglas said:
For some reason it looks like gets is not pausing the script
application and just returning nil.

gets is reading from the file, that's been passed as a command line argument,
and returning nil because it's empty.
 
P

Peter Vanderhaden

Sebastian,
Right you are! When I did the original post, I screwed it up. I know
chomp doesn't get user input, I was using it with gets, and it didn't
work. I've got to do a better job of proofreading before I hit submit.
Sorry!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top