On Tue, 26 Sep 2006 05:49:48 +0900, Robin Stocker wrote:

> Hi all,
> 
> Here's my solution which uses Ruby only and modifies core classes and 
> restores them at the end of the function. I wouldn't trust it ;).
> 
> Maybe this would be a task for Why's Sandbox library? Create a sandbox, 
> modify the core classes there and evaluate the blocks there. It's just 
> an idea, I don't know if it's possible.

Very elegant solution, much more elegant than my own. I'm thinking of
using your code to implement my desired SQL expression generation. I do
suggest blanking the SxpGenerator class first that way built-in methods
will call method_missing (which is the desired behavior).

> class SxpGenerator

[SOME CODE SNIPPED as I want people to see what I wrote and not miss it in
the code.]

>    def self.restore_methods(mod)
>      BINARY_METHODS.each do |method|
>        mod.module_eval do
>          orig_method = "__orig_#{method}__"
>          if method_defined? orig_method

After looking at Borris' post, where he mentioned throwing lots of
warnings, I tested your code with warnings on. You can keep your code from
generating warnings by adding one line here (in the space where this
comment is):

remove_method method

(this way, the alias_method doesn't replace an existing method, thereby
generating warnings)

>            alias_method method, orig_method
>            remove_method orig_method
>          else
>            remove_method method
>          end
>        end
>      end
>    end
> 
> end

[REST OF CODE SNIPPED]

   def test_variables_from_outside
     var=:count
     assert_equal [:-,:count,3], sxp { var-3 }
   end

This test, which your code passes, also happens to be pretty useful for my
purposes.

--Ken

-- 
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
I've added a signing subkey to my GPG key. Please update your keyring.