Analysing ruby code

Hello everybody !

Somebody knows the best way or even a tool to split ruby code into its
different constructs ?
For example:

class Foo
def initialize
@x = “y”
end

def Bar(*args)
if args[0]
puts “Bar” + args[0]
else
puts @x
end
end
end

should be split into an Array

[“class Foo\n”, “def initialize\n\t@x=“y”\nend\n”, “def Bar(*args)\n”,
"if args[0]…

Thanks a lot !

Domenico

On 5/22/07, [email protected] [email protected] wrote:

[“class Foo\n”, “def initialize\n\t@x=“y”\nend\n”, “def Bar(*args)\n”,
"if args[0]…

This looks like you are just splitting it into lines. If so then if
it’s in a file
filename = ‘myprog.rb’
line_array = File.readlines(filename)

Or if it’s a string
source = <<END
class Foo
def initialize
@x = “y”
end

def Bar(*args)
if args[0]
puts “Bar” + args[0]
else
puts @x
end
end
end
END
line_array = source.to_a


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

unknown wrote:

No, it’s a little bit more than just splitting it into lines.
It’s more that I want

[beginning of class, methode 1, methode 2, if-construct in methode 2,
the rest of methode 2, …]

so basicly every methode/class/if/… with their code as an own entry
in an array.

Domenico

You can use the irb tokenizer to do that. I used it to create colored
HTML. It takes a bit reading into it, though. But if you know ruby a bit
it’s doable.

Regards
Stefan

No, it’s a little bit more than just splitting it into lines.
It’s more that I want

[beginning of class, methode 1, methode 2, if-construct in methode 2,
the rest of methode 2, …]

so basicly every methode/class/if/… with their code as an own entry
in an array.

Domenico

On 5/22/07, [email protected] [email protected] wrote:

No, it’s a little bit more than just splitting it into lines.
It’s more that I want

[beginning of class, methode 1, methode 2, if-construct in methode 2,
the rest of methode 2, …]

Maybe ParseTree [1] is what you are looking for. Look at the example
at zenspider projects | software projects | by ryan davis

[1] http://rubyforge.org/projects/parsetree/