From: Jos Backus Date: 2006-08-11T10:33:57+09:00 Subject: Referring to an instance method using a variable As part of a larger program I am trying to convert the following Perl code to Ruby: lizzy:~% cat ptest sub equals { my($a, $b) = @_; return $a eq $b ? 1 : 0; } my $ops = { '=' => sub { my ($a, $b) = @_; return $a eq $b ? 1 : 0; }, '==' => \&equals, }; print $ops->{'='}(1, 1); print $ops->{'=='}(1, 2); lizzy:~% perl -l ptest 1 0 lizzy:~% This is what I have come up with: 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 saying `proc { |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 causes `equals' to be called. (In Python one has to use `equals()' to actually perform the call). Ideas, anybody? Thanks, -- Jos Backus jos at catnook.com