From: Peter Szinek Date: 2008-11-15T20:05:49+09:00 Subject: Re: how to call operator as function --Apple-Mail-7--667966839 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit On 2008.11.15., at 11:56, Vladimir Fekete wrote: > Hi *, > > could you help me please ? I'm trying to write a code where I could > have > operators' functions as values of hash and executing them in way > like this > (formal code): > > #============================= > operators = { "+" => Fixnum.+, > "-" => Fixnum.-, > "++" => Myclass.++} > > op = "+" > > result = operators[op](left,right) if left.class==Fixnum and > right.class==Fixnum > #============================ ++ is not supported by Ruby :) I would do 1.send '+'.to_sym, 2 unless you want to use some esoteric operator mappings, you don't need the hash. Fixnum.+ doesn't make that much sense because of Ruby's duck typing (and why would you want to restrict yourself to work with just certain objects?) p.s.: dobre tu vidiet matfyzaka :-) Cheers, Peter ___ http://www.rubyrailways.com --Apple-Mail-7--667966839--