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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:43:27+00:00 2026-06-18T11:43:27+00:00

In the code below, I read from a redis queue and trigger callbacks on

  • 0

In the code below, I read from a redis queue and trigger callbacks on a local deferrable variable d when the redis operation completes.

It works fine, but I expected to get an error because when the method f returns the variable d goes out of scope. Or does it?

why does this work? is it because the blocks that use d create closures and these closures keep d alive?

require 'em-hiredis'

def f
  d = EM::DefaultDeferrable.new

  EM::Hiredis.connect.blpop('abcdef', 5).
    callback {|x| d.succeed x}.
    errback {|e| d.fail e}

  d.
    callback {|x| puts "value=#{x}"}.
    errback {|e| puts "error #{e}"}

  return nil
end

EM.run {f}

EDIT: Please notice that the EM::Hiredis instance is local too, and the blocks are attached to it. Are not the blocks supposed to go away with the redis connection when f returns?

  • 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-18T11:43:28+00:00Added an answer on June 18, 2026 at 11:43 am

    Ruby blocks are interesting, because they include a binding to the scope they are defined within.

    This means that your deferrable is bound within the block and available to the block, even when the block is executed elsewhere/later.

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

Sidebar

Related Questions

How can I read the data from read stream in Objective-C? The code below
I am using the code below to read data from a excel sheet and
The code below is my attempt to read from a file of strings, read
I'm using the below code to read data from a connected socket and the
I want to modify the below code to read the nodes from a text
I am using the code below to read a sql statement from stdin on
I am using the code below to read the the txt files from my
In the Code below I am trying to recreate scanf to read everything from
I have used the code below to read rfid tag values. try { if
In the code below i`m trying to read a list of selected projects and

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.