Object#to_ruby and Object#to_c

B

Ben Giddings

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
 
I

Ilmari Heikkinen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top