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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:13:14+00:00 2026-05-31T07:13:14+00:00

According to The Ruby Programming Language p.164. If a begin statement doesn’t propagate an

  • 0

According to The Ruby Programming Language p.164.

If a begin statement doesn’t propagate an exception, then the value
of the statement is the value of the last expression evaluated in
the begin, rescue or else clauses.

But I found this behavior consistent with the begin block together with else clause and ensure clause.

Here is the example code:

def fact (n)
  raise "bad argument" if n.to_i < 1
end

value = begin
  fact (1)
rescue RuntimeError => e
  p e.message
else
  p "I am in the else statement"
ensure
  p "I will be always executed"
  p "The END of begin block"
end

p value

The output is:

"I am in the else statement"
"I will be always executed"
"The END of begin block"
"I am in the else statement"
[Finished]

The value is evaluated to the else clause. This is inconsistent behavior as the ensure clause is the last statement executed.

Could someone explain what’s happening within the begin block?

  • 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-31T07:13:16+00:00Added an answer on May 31, 2026 at 7:13 am

    I’d interpret the goal of the begin/rescue/else/end block as:

    • Execute the code in the begin section, and then the code in the else section.
    • If something goes wrong in the begin section, execute the rescue section instead of the else section.

    So either the rescue section or the else section will be executed after trying the begin section; so it makes sense that one of them will be used as the whole block’s value.

    It’s simply a side effect that the ensure section will always be executed.

    val = begin
      p "first"; "first"
    rescue => e
      p "fail"; "fail"
    else
      p "else"; "else"
    ensure
      p "ensure"; "ensure"
    end
    
    val # => "else"
    # >> "first"
    # >> "else"
    # >> "ensure"
    

    But:

    val = begin
      p "first"; "first"
      raise
    rescue => e
      p "fail"; "fail"
    else
      p "else"; "else"
    ensure
      p "ensure"; "ensure"
    end
    
    val # => "fail"
    # >> "first"
    # >> "fail"
    # >> "ensure"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to Charles Nutter , Duby is a static-typed language with Ruby's syntax and
According to this discussion , the iphone agreement says that it doesn't allow loading
According to Wikipedia, on the Comparison of programming languages page, it says that F#
According to the documentation mod.const_get(sym) Returns the value of the named constant in mod.
According to the Ruby Set class's documentation, == Returns true if two sets are
According to the Ruby on Rails 2.3 Release Notes... if your plugin has an
I'd like to: Represent a DAG according to The Ruby Way. Generate an image
according to your experiences what's the better choice to access memcached from ruby? Thank
according to your experiences what's the better choice to access memcached from ruby? Thank
What is the difference between Mixins and Traits? According to Wikipedia , Ruby Modules

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.