lizzy:~% cat rtest
OPS = {
'=' => proc { |a, b| return a == b ? 1 : 0 },
'==' => proc { |a, b| send(:equals, a, b) },
}
def equals(a, b)
return a == b ? 1 : 0
end
puts OPS['='].call(1, 1)
puts OPS['=='].call(1, 2)
lizzy:~% ruby rtest
1
0
lizzy:~%
But the ==' case is rather ugly. Is there a shorter way than sayingproc {
|a, b| send(:equals, a, b) }’? I.e. is there a way to avoid using the proc
wrapper?
I guess one the problems is that unlike in Python, parentheses are
optional
Ruby. This means that equals' returns what I am looking for in Python but in Ruby it causesequals’ to be called. (In Python one has to use
`equals()’ to
actually perform the call).
Is there supposed to be a semantic difference between the ‘=’ and ‘==’
operator in what you are trying to accomplish or are you just trying to
implement ‘=’ as a proc and ‘==’ as a method?
Is there supposed to be a semantic difference between the ‘=’ and ‘==’
operator in what you are trying to accomplish or are you just trying to
implement ‘=’ as a proc and ‘==’ as a method?
The latter. It’s just an example. They are operators in a templating
language
we use at work.