Analysing ruby code

C

cypher.dp

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
 
R

Rick DeNatale

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].........

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
 
C

cypher.dp

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
 
S

Stefan Rusterholz

unknown said:
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
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top