Forum: Ruby what's wrong about this chomp code?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ffd881829651935d986c52d12279befb?d=identicon&s=25 Zhenning Guan (rubyernewbie)
on 2008-11-05 13:52
when I was running this code
------------
File.open("1c.txt") do |f|
  while line= f.gets.chomp;
   p line ;
  end
end
-------------

why they warning with the message below?

=========
private method 'chomp' called for nil:NilClass <nomethoderror>
from ok.rb:2:in 'open'
from ok.rb:2
==========
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2008-11-05 14:01
(Received via mailing list)
Alle Wednesday 05 November 2008, Zhenning Guan ha scritto:
>
> =========
> private method 'chomp' called for nil:NilClass <nomethoderror>
> from ok.rb:2:in 'open'
> from ok.rb:2
> ==========

Because chomp will be called on the value returned by f.gets, even when
it's
nil. You need to move the call to chomp inside the while loop:

while line=f.gets
  line = line.chomp #or simply line.chomp!
  ...

You can also add a rescue clause inside the condition of the while loop:

while (line = f.gets.chomp rescue nil)
  ...

This way, when f.gets returns nil and the NoMethodError exception is
raised,
the exception is rescued and line is set to nil, which causes the loop
to end.

I hope this helps

Stefano
Ffd881829651935d986c52d12279befb?d=identicon&s=25 Zhenning Guan (rubyernewbie)
on 2008-11-05 14:29
Stefano Crocco wrote:
> This way, when f.gets returns nil and the NoMethodError exception is
> raised,
> the exception is rescued and line is set to nil, which causes the loop
> to end.
>
> I hope this helps
>
> Stefano

Thank you for help me understand it. :)
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2008-11-05 15:11
Zhenning Guan wrote:
> when I was running this code
> ------------
> File.open("1c.txt") do |f|
>   while line= f.gets.chomp;
>    p line ;
>   end
> end
> -------------
>
> why they warning with the message below?
>
> =========
> private method 'chomp' called for nil:NilClass <nomethoderror>
> from ok.rb:2:in 'open'
> from ok.rb:2
> ==========


This might be easier:
File.open('1c.txt').each { |line| puts line.chomp }
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-11-05 15:24
(Received via mailing list)
Lloyd Linklater wrote:
> File.open('1c.txt').each { |line| puts line.chomp }

File.foreach('1c.txt') {|line| puts line}

File.foreach is better than File.open(...).each because that way the
file is
closed right away (with your code open returns a file object that you
simply
throw away after calling each on it, so you can't call close on it). And
doing puts file.chomp  is pointless as puts will just readd the \n
removed by
chomp.

HTH,
Sebastian
This topic is locked and can not be replied to.