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 9237111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:27:39+00:00 2026-06-18T07:27:39+00:00

I am reading The Ruby Programming Language and wanted to try the example provided

  • 0

I am reading “The Ruby Programming Language” and wanted to try the example provided in the book (Chapter 6, Altering Control Flow, catch and throw) for using catch/throw to break out of nested loops:

for matrix in data do 
  catch :missing_data do
    for row in matrix do 
      for value in row do
        throw :missing_data unless value
        puts value
      end
    end
  end
end

I defined data as an array of three matrices:

data
=> [[[2, 3, 7], [8, 9, 10], [0, 1, 4]], [[“a”, “b”, “c”], [“d”, false, “e”], [“f”, “g”, nil]], [[“abc”, “def”], [“ghi”, “jkl”]]]

However when I try the code in IRB (version 1.9.3p327), I get an “unexpected $end, expecting keyword_end” error. I’ve tried playing around with the code and have discovered that it works if I remove the outmost (i.e. for) loop, but with the loop I get the error. To double check, I created a simpler loop:

for i in [1,2,3] do
  catch :two do
    if i==2 then throw :two end
  end
end

Again, I get the same situation: error with the for loop, no error without the for loop (in fact, using IRB, I don’t get the chance to enter the last ‘end’, but get the error after entering the fourth line above).

Does anyone know why I am getting this error?

  • 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-06-18T07:27:40+00:00Added an answer on June 18, 2026 at 7:27 am

    This appears to be build and/or environment-dependent (I lean towards environment):

    • Original code works when I execute it from within Sublime Text 2
    • Original code works when running via ruby from command line
    • Original code does not work when I type it in to irb

    Inside irb dropping the do in the outer for, or using {} for the catch, works:

    $ rvm use 1.9.3-p327                                                                              Using /Users/Dave/.rvm/gems/ruby-1.9.3-p327
    $ irb
    irb(main):001:0> data = [[[2, 3, 7], [8, 9, 10], [0, 1, 4]], [["a", "b", "c"], ["d", false, "e"], ["f", "g", nil]], [["abc", "def"], ["ghi", "jkl"]]]
    irb(main):002:0> for matrix in data do
    irb(main):003:1*   catch(:missing_data) {
    irb(main):004:2*     for row in matrix do 
    irb(main):005:3*       for value in row do
    irb(main):006:4*         throw :missing_data unless value
    irb(main):007:4>         puts value
    irb(main):008:4>       end
    irb(main):009:3>     end
    irb(main):010:2>   }
    irb(main):011:1> end
    2
    3
    7
    8
    9
    10
    0
    1
    4
    a
    b
    c
    d
    abc
    def
    ghi
    jkl
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a book called The Ruby Programming Language for Ruby 1.8 and 1.9.
While reading the book Programming Ruby , one example shows how blocks can be
tl:dr how does decoupling work? could need some little example I'm reading Programming Ruby
Hello I'm trying to improve my ruby knowledge by reading The Ruby Programming Language
I just started reading this book Eloquent Ruby and I have reached the chapter
I'm reading through 'the ruby programming language' and have come across the following piece
I'm currently reading The Ruby Programming Language , and I am not sure how
I'm reading The Ruby Programming Language. In section 3.2.6.1, Multibyte characters in Ruby 1.9,
I was reading some examples on symbols in ruby, and one example was using
While reading Programming Ruby , I ran across this code snippet: while gets num1,

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.