# Referring to an instance method using a variable

**URL:** <https://www.ruby-forum.com/t/referring-to-an-instance-method-using-a-variable/102353>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/referring-to-an-instance-method-using-a-variable/102353 "2006-08-15T14:15:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jos\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@Jos\_B](https://www.ruby-forum.com/u/Jos_B)\
**Post date:** [August 15, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/referring-to-an-instance-method-using-a-variable/102353/1 "2006-08-15T14:15:53Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Jos\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@Jos\_B](https://www.ruby-forum.com/u/Jos_B)\
**Post date:** [August 15, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/referring-to-an-instance-method-using-a-variable/102353/2 "2006-08-15T14:15:56Z")

</div>

On Aug 10, 2006, at 9:33 PM, Jos B. wrote:

> ```
> end
> 
> puts OPS['='].call(1, 1)
> puts OPS['=='].call(1, 2)
> lizzy:~% ruby rtest
> 1
> 0
> lizzy:~%
> 
> ```

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?

Does this help at all?

def equals(a, b)  
return a == b ? 1 : 0  
end

OPS = {  
‘=’ =\> proc { |a, b| return a == b ? 1 : 0 },  
‘==’ =\> method(:equals),  
}

puts OPS[’=’].call(1, 1)  
puts OPS[’==’].call(1, 2)

Gary W.

---

<div class="post-metadata">

**Author:** ![Jos\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@Jos\_B](https://www.ruby-forum.com/u/Jos_B)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/referring-to-an-instance-method-using-a-variable/102353/3 "2006-08-15T14:16:02Z")

</div>

On Fri, Aug 11, 2006 at 10:56:45AM +0900, removed\_email\_address@domain.invalid wrote:

> 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.

> }
> 
> puts OPS[’=’].call(1, 1)  
> puts OPS[’==’].call(1, 2)

It sure does. But I could have sworn I tried that, which is why I posted  
:-/  
Guess not.

Thanks Gary!
