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

  • Home
  • SEARCH
  • 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 8794789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:17:57+00:00 2026-06-13T23:17:57+00:00

I occasionally see begin…end blocks used in ruby without any rescue , else ,

  • 0

I occasionally see begin...end blocks used in ruby without any rescue, else, ensure, etc. statements in between. For example:

foo = begin
   whatever = 3
   "great"
   42
end

The coder’s intent, it seems, is to use the begin...end block just for its block-grouping quality (as if begin were do). Personally I think this usage kind of violates the principle of least surprise (begin implies exception-handling to me).

Are there any unintended consequences of using begin...end in this way? Do begin...end blocks have any semantic differences (maybe in exception-handling?) that make this usage dangerous?

Ruby’s syntax is unbelievably subtle, and I wouldn’t be surprised if there were weird gotchas lying in wait here.

  • 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-13T23:17:57+00:00Added an answer on June 13, 2026 at 11:17 pm

    I use this sometimes if I want to assign something to a variable but I have to calculate the value I want to assign first. It makes the code a little bit more tidy this way. I think it’s user preference. Basically you are saying: I am assigning something to foo, but in order to get the value I want I first need to do some things. It’s particularly useful when doing memoization, so instead of

    if @cache.nil?
      do_something!
      @cache = read_value
    end
    

    You can do

    @cache ||= begin
      do_something!
      read_value
    end
    

    What you are taking advantage here is that the Ruby interpreter has a stack, and each expression will usually push something on the stack, or take something from the stack. Assignment just takes the last thing from the stack and assigns it (in this case the last line from begin/end). Many times knowing this (stack approach in Ruby) can be useful.

    I don’t think it violates least surprise though, I think it’s user preference wheather you want to use it or not.

    You can see that it doesn’t do anything unexpected by looking at what bytecode instructions it generates in Ruby MRI 1.9:

     RubyVM::InstructionSequence::compile("c = begin; a = 5; 6; end").to_a
    
     [:trace, 1],
     [:trace, 1],
     [:putobject, 5],
     [:setlocal, 2],
     [:trace, 1],
     [:putobject, 6],
     [:dup],
     [:setlocal, 3],
     [:leave]
    

    Trace is just for stack traces, you can ignore that. Dup duplicates the last item on the stack. In this example the number of the local variable a is 2 and the number of the local variable c is 3 (hence putobject, 2 will assign to variable a, etc).
    The only side-effect of this compared to a = 5; c = 6 is the dup instruction, which means the stack size of your method will be larger by 1 slot. But this is not particularly important because it only has any effect while the interpreter is inside this particular method, and memory for stack is pre-reserved anyway, so it only means the stack pointer will be decremented by 1 more than it would otherwise. So basically no change at all. With optimizations turned on even the dup will probably disappear.

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

Sidebar

Related Questions

I occasionally see the list slice syntax used in Python code like this: newList
Possible Duplicate: PHP method chaining? I occasionally see some php applications use classes like
My app is crashing occasionally and when I see the crash logs this is
I always see people in serious projects use :: everywhere, and -> only occasionally
I occasionally see portraits and other images which have been redrawn into an abstract
Ever since switching to Mac based RoR development, I occasionally see that Webrick dies
I'm not really sure if ERD is the proper term, but occasionally I see
In our VB.net codebase I occasionally see Dim x as new Y() , where
In our VB.net codebase I occasionally see Dim x as new Y() , where
Occasionally when I'm compiling, I have to scroll up my compilation buffer to see

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.