[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

chomp - works in irb, not in program

Peter Vanderhaden

11/23/2007 4:18:00 PM

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....
--
Posted via http://www.ruby-....

20 Answers

Sebastian Hungerecker

11/23/2007 4:36:00 PM

0

Peter Vanderhaden 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.


--
NP: Anathema - Balance
Jabber: sepp2k@jabber.org
ICQ: 205544826

Douglas Greenshields

11/23/2007 4:38:00 PM

0

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


Peter Vanderhaden 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....

--
Posted via http://www.ruby-....

Peter Vanderhaden

11/23/2007 4:45:00 PM

0

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

--
Posted via http://www.ruby-....

Peter Vanderhaden

11/23/2007 4:48:00 PM

0

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

--
Posted via http://www.ruby-....

Sebastian Hungerecker

11/23/2007 5:23:00 PM

0

Peter Vanderhaden 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
--
NP: In Flames - Black & White
Jabber: sepp2k@jabber.org
ICQ: 205544826

Douglas Greenshields

11/23/2007 5:41:00 PM

0

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.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

11/23/2007 5:45:00 PM

0

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.


--
NP: Moonsorrow - Unohduksen lapsi
Jabber: sepp2k@jabber.org
ICQ: 205544826

Peter Vanderhaden

11/23/2007 6:38:00 PM

0

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 Hungerecker wrote:
> Peter Vanderhaden 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.

--
Posted via http://www.ruby-....

MattB+

11/2/2013 5:17:00 PM

0

On Sat, 2 Nov 2013 17:10:12 +0000 (UTC), "BeeSting Alergy"
<BeeSting_Alergy@aol.com> wrote:

>Ron wrote:
>
>> On Saturday, November 2, 2013 10:51:47 AM UTC-5, BeeSting Alergy
>> wrote:
>> > Nov 02, 2013
>>
><horrible line breaks (my posted article) deleted>

BeeSting do you know what NG "Ron" post from I'm only getting one side
of the discussion yours? I normally read "alt.society.liberalism"

Sorry for interrupting.

>>
>> O'Reilly wrote: "According to the Census Bureau, more people in
>> America today are on welfare than have full-time jobs."
>>
>> Bill O'Reilly is full of dookie....as usual.
>>
>> Here are some facts about the subject of welfare versus employed:
>>
>> http://www.politifact.com/truth-o-meter/statements/2013/oct/...
>> -sykes/charlie-sykes-says-today-there-are-more-people-wel/
>>
>> http://www.snopes.com/politics/taxes/death...
>>
>> And here's another myth from Chain E-Mail:
>>
>> http://www.politifact.com/texas/statements/2013/jan/11/chain...
>> in-email-says-11-states-have-more-people-welfar/
>>
>> Why do you swallow rumors so easily?
>
>Why do you, moron?
>
>In the first article to which you referred:
>
>[...]
>"We asked Sykes where he got his information and he sent us a link to
>an article that appeared on the CNS News website. CNS News is a project
>of the Media Research Center, a conservative group that aims to counter
>what it sees as liberal bias in the media."
>[...]
>
>Then it says: "Correction: An earlier version of this fact-check
>WRONGLY IDENTIFIED the Media Research Center."
>
>If the article was wrong about something that is easily researched,
>what makes you think the rest of the article is "fool-proof", idiot?
>
>Just so you know.

Yoorghis

11/2/2013 5:32:00 PM

0

On Sat, 02 Nov 2013 10:17:06 -0700, MattB+ <Trdell1234@--gmail.com>
wrote:

>BeeSting do you know what NG "Ron" post from I'm only getting one side
>of the discussion yours? I normally read "alt.society.liberalism"

IF it don't have "gangs" in it---why do you care?