Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Referring to an instance method using a variable

Reply
Thread Tools

Referring to an instance method using a variable

 
 
Jos Backus
Guest
Posts: n/a
 
      08-11-2006
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

 
Reply With Quote
 
 
 
 
gwtmp01@mac.com
Guest
Posts: n/a
 
      08-11-2006

On Aug 10, 2006, at 9:33 PM, Jos Backus wrote:
> 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:~%


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 Wright




 
Reply With Quote
 
 
 
 
Jos Backus
Guest
Posts: n/a
 
      08-11-2006
On Fri, Aug 11, 2006 at 10:56:45AM +0900, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

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


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

Thanks Gary!

--
Jos Backus
jos at catnook.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referring to the class name from a class variable where inheritanceis involved Paul Moore Python 5 12-06-2011 06:59 PM
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
[Newbie] Referring to a global variable inside a function =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?= Python 4 04-10-2006 09:58 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM



Advertisments