[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Analysing ruby code

cypher.dp

5/22/2007 7:44:00 PM

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

4 Answers

Rick DeNatale

5/22/2007 8:06:00 PM

0

On 5/22/07, cypher.dp@gmail.com <cypher.dp@gmail.com> wrote:
> 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

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

cypher.dp

5/22/2007 8:19:00 PM

0

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

Stefan Rusterholz

5/22/2007 8:26:00 PM

0

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

--
Posted via http://www.ruby-....

Luis Parravicini

5/22/2007 8:34:00 PM

0

On 5/22/07, cypher.dp@gmail.com <cypher.dp@gmail.com> 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 http://www.zenspider.com/ZSS/Products/...

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


--
Luis Parravicini
http://ktulu.co...