Object#to_ruby and Object#to_c

In case you missed it, on RedHanded, I spotted was an interesting snippet I didn't see on the list or anything:

It's a way of using ParseTree and RubyToC to add 'to_c' and 'to_ruby' methods to any Method.

http://redhanded.hobix.com/cult/tooInterestingToPassUp.html

(it also kinda shows how difficult it would be to truly obfuscate Ruby source)

A teaser:

class Example
   def example(arg1)
     return "Blah: " + arg1.to_s
   end
end

quoting zenspider:

Source:

e = Example.new
puts "sexp:"
p e.method(:example).to_sexp
puts "C:"
puts e.method(:example).to_c
puts "Ruby:"
puts e.method(:example).to_ruby

Output:

sexp:
[:defn, :example, [:scope, [:block, [:args, :arg1], [:return, [:call, [:str, "Blah: "], :+, [:array, [:call, [:lvar, :arg1], :to_s]]]]]]]
C:
str
example(long arg1) {
return strcat("Blah: ", to_s(arg1));
}
Ruby:
def example(arg1)
return "Blah: " + arg1.to_s
end

Ben

Hi,

In case you missed it, on RedHanded, I spotted was an interesting snippet I didn't see on the list or anything:

It's a way of using ParseTree and RubyToC to add 'to_c' and 'to_ruby' methods to any Method.

http://redhanded.hobix.com/cult/tooInterestingToPassUp.html

I hacked some on the original RubyToRuby class of Ryan Davis. Now it parses most of ruby syntax, including itself.

http://dark.fhtr.org/ruby2ruby.rb

There's a test unit at the bottom of the file to demonstrate the capability

Interesting thoughts arose:
1. docstrings would be quite trivial to implement with parsetree
2. one could write ruby-lisp that eats parsetree, and translate it to ruby and vice versa

-Ilmari

···

On 8.2.2005, at 19:52, Ben Giddings wrote:

Oh, forgot to mention, it does indentation too :slight_smile:

http://dark.fhtr.org/generated_source.rb

The bit to make it print

def foo
   5
rescue
   10
end

as such instead of

def foo
   5
   rescue
     10
end

made it kinda messy though, but such is life...

···

On 13.2.2005, at 18:36, Ilmari Heikkinen wrote:

I hacked some on the original RubyToRuby class of Ryan Davis. Now it parses most of ruby syntax, including itself.