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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:02:13+00:00 2026-05-26T21:02:13+00:00

Ruby and StackOverflow newb here working my way through Ruby and ran into my

  • 0

Ruby and StackOverflow newb here working my way through Ruby and ran into my first major roadblock. I’m having a really hard time wrapping my head around Procs and Lambdas. Here is the code I’m working with.

def procBuilder(message)
  Proc.new{ puts message; return}
end

def test
  puts "entering method"
  p = procBuilder("entering proc")
  p.call
  puts "exit method"
end

test

By design, this is to throw a LocalJumpError, but I don’t rightly understand why. If I had to guess what this did, I would guess it would initially print “entering proc” upon p = procBuilder(“entering proc”) running then throw an error on p.call as there is no string being passed by p.call, but clearly I’m missing something critical that is occurring between those 2 lines. I also don’t completely understand why this works with a lambda rather than a proc but I imagine understanding the error will resolve that issue as well.

Thanks in advance for the clarification

  • 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-26T21:02:13+00:00Added an answer on May 26, 2026 at 9:02 pm

    Here’s an answer I gave to a related question. It talks a bit about lambda vs proc and LocalJumpErrors.

    In a proc, return is a special piece of syntax that returns from the lexical scope of the proc, not the proc itself. So it’s trying to return out of procBuilder, which has already exited.

    There are a couple ways to fix this:

    1. Don’t use return at all. Ruby will return control to proc’s caller all on its own.
    2. Change proc to lambda, which behaves the way you expect. Lambdas act like methods; procs act like blocks.

    As for the error you’re expecting, you shouldn’t get that. procBuilder returns a proc that encloses the message variable. You don’t need any arguments to the proc itself.

    Edit: answering your additional question. The proc is a closure. It has “captured” the message variable (a local variable in procBuilder), which was in scope when the proc was created. The proc now can wander through your program with the message variable hidden inside of it, ready to be printed when you call it. The only trouble is the return statement, which has the additional requirement that it the lexical scope still be “live”.

    The reason for all this is that this behavior is really helpful in blocks. In this case, it’s not helpful at all, so you should just use a lambda, where return means something less insane.

    A really great tutorial on closures in ruby: http://innig.net/software/ruby/closures-in-ruby.rb

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

Sidebar

Related Questions

I'm new to ruby and this is my first stackoverflow question (although I've used
Now this is my first question here. I'm trying to install Ruby On Rails
I am having a hard time trying to reinstall Ruby (1.9.2-p136) on my ubuntu
I am currently making my way through the Ruby on Rails tutorial over at
There are a couple of questions on Stackoverflow asking whether x ( Ruby /
I just got referred to stackoverflow by a friend here to help with a
I have unfortunately wandered into a situation where I need regex using Ruby. Basically
I am trying to install vim with the ruby interpreter. Following this stackoverflow question
I'm aware of the Ruby word array: a = %w( stackoverflow serverfault superuser) How
When I first heard about StackOverflow, and heard that it was being built in

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.