It made me go crazy, I post here after looking around a lot.
I would like to know if two variables point to the same Proc to the same Proc. I am sure this should be something that I am not getting, for example, why do they all 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)
Thanks Mark, you made it a lot clearer. In the previous one, each time it returned new objects, so that they are equal? the function will never return to the truth. The two lambdas were functionally the same, but not the same object. Therefore, if you create one version and then return it back to the method, you can check its identifier. The following makes sense and works as I expected.
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)
puts func1.equal?(func3)
puts func1.equal?(LambdaFunctions.LambdaFunction1)
puts func3.equal?(LambdaFunctions.LambdaFunction1)
puts func3.equal?(LambdaFunctions.LambdaFunction2)
source
share