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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:49:36+00:00 2026-05-16T14:49:36+00:00

I have started learning Ruby, and I have read a couple of tutorials and

  • 0

I have started learning Ruby, and I have read a couple of tutorials and I even bought a book (“Programming Ruby 1.9 – The Pragmatic Programmers’ Guide”), and I came across something new that I haven’t seen before in any of the other languages I know (I am working as a PHP webdeveloper).

Blocks & Procs.
I think I understand what they are, but what I don’t understand is why they are so great, and when and why I should use them.
Everywhere I look they say blocks and procs are a great feature in Ruby, but I don’t understand them.

Can anybody here give a total Ruby-newbie like me some explanations?

  • 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-16T14:49:37+00:00Added an answer on May 16, 2026 at 2:49 pm

    There are a lot of things that are good about blocks. The elevator pitch: Blocks let us pass around actions the same way we normally pass around data.

    The most obvious level is that they let you abstract things out into functions that wouldn’t be possible otherwise. For example, let’s look at a common case where you have a list of things and you want to filter it to only include items that match some criterion:

    int list[50] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
    int evenNumbers[50] = {0};
    int copyIndex = 0;
    for (int i = 0; i < 50; i++) {
        if (list[i] % 2 == 0) {
            evenNumbers[copyIndex++] = list[i];
        }
    }
    

    Here’s how you write that in Ruby:

    list = 1..50
    listCopy = list.select {|n| n.even?}
    

    All the common busywork is moved out of your code and into a method with a meaningful name. We don’t care about copying the array and going through indexes and all that — we just want a filtered list. And that’s what select gives us. The block allows us to pass our custom logic into this standard method.

    But iterators aren’t the only place where this “hole in the middle pattern” is useful. For example, if you pass a block to File.open, it will open the file, execute the block with the file and then close the file for you.

    Another thing that blocks give us is a really powerful form of callbacks. For example, without blocks, we might have to do something like this (based on how dialogs actually work in Objective-C Cocoa):

    class Controller
      def delete_button_clicked(item)
        item.add_red_highlight
        context = {:item => item}
        dialog = Dialog.new("Are you sure you want to delete #{item}?")
        dialog.ok_callback = :delete_OK
        dialog.ok_receiver = self
        dialog.cancel_callback = :cancel_delete
        dialog.cancel_receiver = self
        dialog.context = context
        dialog.ask_for_confirmation
      end
    
      def delete_OK(sender)
        delete(sender.context[:item])
        sender.dismiss
      end
    
      def cancel_delete(sender)
        sender.context[:item].remove_red_highlight
        sender.dismiss
      end
    end
    

    Yowza. With blocks, we could do this instead (based on a common pattern used in many Ruby libraries):

    class Controller
      def delete_button_clicked(item)
        item.add_red_highlight
        Dialog.ask_for_confirmation("Are you sure you want to delete #{item}?") do |response|
          response.ok { delete item }
          response.cancel { item.remove_red_highlight }
        end
      end
    end
    

    That’s actually two levels of blocks — the do...end block and the two {}-style blocks. But it reads pretty naturally, doesn’t it? This works because a block captures the context it’s created in, so we don’t need to pass around self and item.

    As for Procs, they’re just an object wrapper for blocks. Not very much to them.

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

Sidebar

Related Questions

I have recently started learning Ruby, as my first programming language. I feel comfortable
I have started learning VB.NET lately and have read 1 Step-By-Step beginners book and
I have just started learning struts. Most of the tutorials I read, it mentions
I have started learning Ruby and just tried out my first hello world program
I have started learning Ruby recently and I was trying out the following piece
I have started learning Ruby for the past 2,3 weeks and I have come
After learning power of Rails framework, I have started learning ruby. In Ruby, I
I have just started learning ruby on rails. I have a doubt wrt routing.
I have just started learning Ruby on Rails. I happened to look for prevention
I have just started learning ruby reading from different resources. One of them is

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.