# What's wrong about this chomp code?

**URL:** <https://www.ruby-forum.com/t/whats-wrong-about-this-chomp-code/152596>\
**Category:** Ruby\
**Created:** [November 5, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/whats-wrong-about-this-chomp-code/152596 "2008-11-05T13:52:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 5, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/whats-wrong-about-this-chomp-code/152596/1 "2008-11-05T13:52:53Z")

</div>

## 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 from ok.rb:2:in ‘open’ from ok.rb:2

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 5, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/whats-wrong-about-this-chomp-code/152596/2 "2008-11-05T14:01:02Z")

</div>

Alle Wednesday 05 November 2008, Zhenning G. ha scritto:

> # ========= private method ‘chomp’ called for nil:NilClass 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

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 5, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/whats-wrong-about-this-chomp-code/152596/3 "2008-11-05T14:29:21Z")

</div>

Stefano C. 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. 🙂

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 5, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/whats-wrong-about-this-chomp-code/152596/4 "2008-11-05T15:24:44Z")

</div>

Lloyd L. 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

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 5, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/whats-wrong-about-this-chomp-code/152596/5 "2008-11-05T15:11:56Z")

</div>

Zhenning G. 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 from ok.rb:2:in ‘open’ from ok.rb:2

This might be easier:  
File.open(‘1c.txt’).each { |line| puts line.chomp }
