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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:08:09+00:00 2026-06-05T08:08:09+00:00

In Eloquent Ruby there’s a code example I don’t understand. class Document attr_accessor :save_listener

  • 0

In Eloquent Ruby there’s a code example I don’t understand.

class Document
  attr_accessor :save_listener

  # most of the class omitted...

  def on_save( &block )
    @save_listener = block
  end

  def save( path )
    File.open( path, 'w' ) { |f| f.print( @contents ) }
    @save_listener.call( self, path ) if @save_listener
  end
end

# usage
my_doc = Document.new( 'block based example', 'russ', '' )
my_doc.on_save do |doc|
  puts "Hey, I've been saved!"
end

Why is it that @save_listener.call( self, path ) takes TWO arguments? The block that’s saved looks like it only has ONE parameter |doc|. Is this a typo in the book or is there something here I’m missing?

I even tried typing this code in and executing it and I found I can add as many parameters as I want and there wont be any errors. But I still don’t get why there are TWO parameters in this example.

  • 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-05T08:08:10+00:00Added an answer on June 5, 2026 at 8:08 am

    This is due to a subtle difference between Proc and Lambda. When you create a new Proc with a block of code, you can pass as many arguments as you’d like to it when you call it. For instance:

    proc = Proc.new {|a,b| a + b}
    proc.arity #=> 2 -- the number of arguments the Proc expects
    proc.call(4,8) #=> 12
    proc.call(4,8,15,16,23,42) #=> 12
    

    It is taking in those arguments but just not assigning them to any of the variables in your block.

    However, a Lambda cares about the number of arguments.

    proc = lambda {|a,b| a + b}
    proc.arity #=> 2
    proc.call(4,8) #=> 12
    proc.call(4,8,15,16,23,42) #=> ArgumentError: wrong number of arguments (6 for 2)
    

    The reason for this is because Proc.call assigns the method’s arguments similar to parallel assignment of variables.

    num1, num2 = 1,2 #=> num1 is 1, num2 is 2
    num1, num2 = 1  #=> num1 is 1, num2 is nil
    num1, num2 = 1,2,3,4,5 #=> num1 is 1, num2 is 2 and the rest are discarded
    

    However, Lambda does not work like this. Lambda acts more like a method call than a variable assignment.

    So, if you are worried about only allowing a certain number of arguments, use Lambda. However, in this example, since there is a chance you can add a path to the block, a Proc is best.

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

Sidebar

Related Questions

I'm reading the Eloquent Ruby book (awesome book so far) and don't understand one
So I'm looking at some code from Eloquent Javascript and it says that to
The following snippet of code is taken from Eloquent JavaScript. var noCatsAtAll = {};
Here is a JavaScript example from Eloquent JavaScript : function findSequence(goal) { function find(start,
I am working my way through Eloquent Javascript and I came across a code
I just started reading this book Eloquent Ruby and I have reached the chapter
Simple one, I was just wondering if there is a clean and eloquent way
I was reading Eloquent JavaScript and I came across this example for the puzzle:
I'm working my way through the Eloquent JavaScript Book and in it there is
My problem is twofold: 1) I'm trying to determine an eloquent way to allow

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.