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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:33:04+00:00 2026-05-24T07:33:04+00:00

Taken from Programming ruby 1.9 book: def my_while(cond, &body) while cond.call body.call end end

  • 0

Taken from Programming ruby 1.9 book:

def my_while(cond, &body)
 while cond.call
   body.call
 end
end

a=0

my_while -> { a < 3 } do
  puts a
  a += 1
end

produces:

0

1

2

  • 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-24T07:33:04+00:00Added an answer on May 24, 2026 at 7:33 am

    The method expects an explicit parameter cond, and this "condition" is assumed to be a lambda/proc (the assumption is made by relying on cond.call to succeed) and has to be passed to the method my_while explicitly. The & syntax captures a method’s block (if present) in a variable by implicitly converting it to a Proc object (see ‘The ampersand’).

    Blocks are not real objects in Ruby and thus have to be converted by using the ampersand syntax. Once the block is bound to a Proc, you can send the call message on it as on any other proc/lambda.

    The -> syntax is short for lambda, which converts a block to a Proc object (explicitly). There is also a slight difference between using lambda and Proc.new. Again, the wikibook:

    Actually, there are two slight differences between lambda and Proc.new.

    First, argument checking. The Ruby documentation for lambda states: Equivalent to Proc.new, except the resulting Proc objects check the number of parameters passed when called.

    Second, there is a difference in the way returns are handled from the Proc. A return from Proc.new returns from the enclosing method (acting just like a return from a block, more on this later):

    def try_ret_procnew
      ret = Proc.new { return "Baaam" }
      ret.call
      "This is not reached"
    end
    
    # prints "Baaam"
    puts try_ret_procnew
    

    While return from lambda acts more conventionally, returning to its caller:

    def try_ret_lambda
      ret = lambda { return "Baaam" }
      ret.call
      "This is printed"
    end
    
    # prints "This is printed"
    puts try_ret_lambda
    

    With this in light, I would recommend using lambda instead of Proc.new, unless the behavior of the latter is strictly required. In addition to being way cooler a whopping two characters shorter, its behavior is less surprising.

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

Sidebar

Related Questions

The following code is taken from Programming in Scala book by Martin Odersky et
This example is taken from the Linux System Programming book, page 88. Code: http://pastebin.com/mEfmHbPP
Can the performance of this sequential search algorithm (taken from The Practice of Programming
Taken from wikipedia: In object-oriented computer programming, a factory is an object for creating
This is code from the day 3 of the Ruby section of 7 programming
This is taken right from The C++ Programming Language by Bjarne Stroustrup. I would
Example taken from Mozilla's help page <script type="text/javascript"> re = /(\w+)\s(\w+)/; str = "John
This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives
This example is taken from w3schools . CREATE TABLE Persons ( P_Id int NOT
Say I have enum as follows (taken from MSDN example): enum Days {Sat=1, Sun,

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.