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…
Peter V. wrote:
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.
How are you using it in the program? Are you using gets.chomp? Can you
show a code sample?
Peter V. wrote:
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…
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 Greenshields wrote:
How are you using it in the program? Are you using gets.chomp? Can you
show a code sample?
Peter V. wrote:
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…
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 V. wrote:
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 Greenshields wrote:
How are you using it in the program? Are you using gets.chomp? Can you
show a code sample?
Peter V. wrote:
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…
Peter V. wrote:
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
Douglas Greenshields wrote:
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.
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.
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!
Sebastian H. wrote:
Peter V. wrote:
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.