On Sat, Jan 26, 2013 at 6:12 PM, Nokan Emiro <[email protected]> wrote:
> Hi Robert,
>
>> Why do you think you need to redefine behavior of these operators?
>
> Because I'm working on a type that behaves like any other Ruby class (in
> terms that it accepts all operations and messages that a Numeric, an Array,
> a Hash or anything else), but it collects all it's history into a huge Ruby
> expression for further evaluation.  It works charmingly with the normal
> ops like :+, :-, :*, :/, :[], :<< etc., but, of course, it fails with those
> outside
> of an object's own world.
>
> irb(main):001:0> x = Accumulator.new :x
> => <<<  x  >>>
> irb(main):002:0> y = x + 123
> => <<<  x + 123  >>>
> irb(main):003:0> z = 2 * y
> => <<<  2 * (x + 123)  >>>
> irb(main):004:0> q = 1 - z ** 2
> => <<<  1 - (2 * (x + 123)) ** 2  >>>
> irb(main):005:0> q+z+y
> => <<<  1 - (2 * (x + 123)) ** 2 + 2 * (x + 123) + x + 123  >>>
> irb(main):006:0>

Ah, I see.

> It may be useful to drop in "spy objects" (like the x above) into black box
> functions,
> like crypto functions, hash calculations, numerical approximations, or even
> well-known
> calculations, just to wrap them up in a huge expression instead of looking
> at them as
> the code of a long method, or just look for algebric simplifications on the
> result.  It can
> help improving the original calculation or finding out more about the
> method's internals.

Are you aware of set_trace_func?  That can also be used for spying
although getting at return values is at least difficult.

I'm afraid, without hacking the interpreter you probably won't be able
to change behavior of && and ||. The only other alternative I can
think of right now is to parse the Ruby code yourself and execute the
AST "manually".

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en


Reply via email to