Ruby2Ruby

Posted by Matt Williams

Ruby2Ruby is a gem which allows you to look at the code of a class. To install do:
gem install ruby2ruby
(you may need to do a sudo in front of it)

Here's an example of its use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'ruby2ruby'
=> true
irb(main):003:0> class Foo
irb(main):004:1> def fud
irb(main):005:2> puts "groo"
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> Ruby2Ruby.translate Foo
=> "class Foo < Object\n  def fud\n    puts(\"groo\")\n  end\nend"
irb(main):009:0> class Foo
irb(main):010:1> def fi
irb(main):011:2> puts "fo"
irb(main):012:2> end
irb(main):013:1> end
=> nil
irb(main):014:0> Ruby2Ruby.translate Foo
=> "class Foo < Object\n  def fi\n    puts(\"fo\")\n  end\n  \n  def fud\n    puts(\"groo\")\n  end\nend"
irb(main):015:0> class Foo
irb(main):016:1> def fud
irb(main):017:2> puts "Hello World!"
irb(main):018:2> end
irb(main):019:1> end
=> nil
irb(main):020:0> Ruby2Ruby.translate Foo
=> "class Foo < Object\n  def fi\n    puts(\"fo\")\n  end\n  \n  def fud\n    puts(\"Hello World!\")\n  end\nend"
irb(main):021:0>

Notice how it changes the class output. One thing I have found is that if you create instance variables outside of a method they won't show up in the output. Otherwise, it works quite well and is a good addition to a metaprogramming toolkit.

Comments

Leave a response