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

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

···

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

--
Rick DeNatale

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

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

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-forum.com/\.

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/

···

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

--
Luis Parravicini
http://ktulu.com.ar/blog/