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