Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:22:32+00:00 2026-05-26T13:22:32+00:00

I introduced the bug shown below into my code the other day and wanted

  • 0

I introduced the bug shown below into my code the other day and wanted to better understand the implications.

File.open('test.txt').readlines do |line|
  puts "#{line} test"
end

File.open.readlines does exactly as expected and returns an array containing all of the lines in the file. But immediately following that array is a block. The array returned by readlines calls no member methods, each for instance. I’m assuming the block has nothing calling it, therefore it does nothing. In other languages this may have thrown a compiler warning or in C the block would be considered a nested scope and it would execute. But as shown, Ruby (1.9.2) happily runs with no errors, and no output generated by puts.

For completeness here’s the corrected version.

File.open('test.txt').each_line do |line|
  puts "#{line} test"
end 

I would like to understand the behavior of the first example. Is my assumption correct in that the block is essentially anonymous and didn’t execute because nothing called it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:22:33+00:00Added an answer on May 26, 2026 at 1:22 pm

    Here is what is happening in your particular example. When you call

    File.open('test.txt').readlines do |line|
      puts "#{line} test"
    end
    

    The block is being passed to the readlines method. The block is not passed to the array that is returned by the readlines method. This is a valid case and thus the ruby interpreter does not complain.

    Here is a simpler scenario to illustrate this

    Let’s say I had a class defined as follows

     class Foo
         def bar
         end 
     end
    

    Now if I call

     Foo.new.bar {puts "hello"}
    

    There will be no error from the interpreter and the “hello” will not be printed.

    If I yield to the block in bar as follows

     class Foo
         def bar
             yield
         end 
     end
    

    Then the puts will be executed and you should see hello printed

     Foo.new.bar {puts "hello"} # prints hello
    

    In summary blocks are passed to methods not objects and you can pass a block to any method in ruby even one which does not expect it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just introduced a bug into my code because I seem to have misunderstood
Upgrading to XCode 3.2.3 and iPhone 3.2/4.0 SDK has introduced a strange bug into
I have a problem that someone introduced a bug but I do not know
I was just made aware of a bug I introduced, the thing that surprised
There is apparently a bug introduced in the latest Java update for Mac OS
I have found a very subtle bug in my R code just now. The
I recently introduced multi-stroke gestures into my application. This is a preference so I
I'm chasing a bug in Perl code that seems to fundamentally be a version
I recently introduced a bug that only rears its head in development due to
Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.