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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:07:39+00:00 2026-05-15T20:07:39+00:00

This has been driving me nuts, I’m posting here after a lot of looking

  • 0

This has been driving me nuts, I’m posting here after a lot of looking around.

I’d like to know if two variables pointing to the same Proc are the pointing to the same Proc.
I’m sure it must be something I’m not getting so for example why do all of these return false?

class LambdaFunctions
  def self.LambdaFunction1
    lambda { |t| t ** 2}
  end
end

a = LambdaFunctions.LambdaFunction1
b = LambdaFunctions.LambdaFunction1

puts LambdaFunctions.LambdaFunction1
puts a
puts b

puts a == b
puts a === b
puts a.eql?(b)
puts a.equal?(b)
puts a == LambdaFunctions.LambdaFunction1
puts a === LambdaFunctions.LambdaFunction1
puts a.eql?(LambdaFunctions.LambdaFunction1)
puts a.equal?(LambdaFunctions.LambdaFunction1)

Thank you Mark, you made it a lot clearer.
In previous it was returning new objects each time so the equal? function was never going to return true. The two lambdas were functionally the same but not the same object.
So if you create one version and then return it back in the method you can test it’s identity.
The following makes more sense and works as I intended.

class LambdaFunctions

  @lambda1 = lambda { |t| t ** 2}
  @lambda2 = lambda { |t| t ** 2}

  def self.LambdaFunction1
    @lambda1
  end

  def self.LambdaFunction2
    @lambda2
  end
end

func1 = LambdaFunctions.LambdaFunction1
func2 = LambdaFunctions.LambdaFunction1
func3 = LambdaFunctions.LambdaFunction2

puts func1.equal?(func2) # true
puts func1.equal?(func3) # false
puts func1.equal?(LambdaFunctions.LambdaFunction1) # true
puts func3.equal?(LambdaFunctions.LambdaFunction1) # false
puts func3.equal?(LambdaFunctions.LambdaFunction2) # true
  • 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-15T20:07:40+00:00Added an answer on May 15, 2026 at 8:07 pm

    While the lambda is effectively equivalent, each call to LambdaFunctions.LambdaFunction1 returns a new instance of the lambda. It would make sense for procs to be equivalent only by identity, not by value, since it would be virtually impossible to determine programmatic equivalence.

    What I mean is, if procs could be determined to be equivalent based on what they did, then lambda { 3 } and lambda { 1 + 2 } would be equivalent. With lambdas any more complicated than that, to determine equivalence would basically require a solution to the halting problem.

    To determine equivalent identity (per the comment), refer to Object#equal?:

    Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the same object as b).

    If you really need a and b to be the same object, then you need to return the same lambda every time; this means you need to assign the lambda to an instance variable in the LambdaFunctions class, for instance, and return that.

    ruby-1.9.1-p378 > class LambdaFunctions
    ruby-1.9.1-p378 ?>   @func1 = lambda { |t| t ** 2 }
    ruby-1.9.1-p378 ?>   def self.LambdaFunction1
    ruby-1.9.1-p378 ?>      @func1
    ruby-1.9.1-p378 ?>    end
    ruby-1.9.1-p378 ?>  end
     => nil 
    ruby-1.9.1-p378 > a = LambdaFunctions.LambdaFunction1
     => #<Proc:0x0000010099e370@(irb):10 (lambda)> 
    ruby-1.9.1-p378 > b = LambdaFunctions.LambdaFunction1 # same address as a
     => #<Proc:0x0000010099e370@(irb):10 (lambda)> 
    ruby-1.9.1-p378 > a == b
     => true 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been driving me absolutely nuts. I have a substitute function like this:
this has been driving me nuts because i know theres a simple solution but
This has been driving me nuts for the past hour. I have two computers,
This has been driving me nuts. I keep getting the following exception System.InvalidOperationException: The
This has been driving me nuts. I hope it's not been asked before but
This has been driving me nuts for a week now. I have a class
This has been driving me nuts all day. I've seen this problem crop up
This has been driving me nuts all day. I have a weird bug that
This one has been driving me nuts. I have a div that has it's
This has been driving me nuts for a few days. I am desperate for

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.