Forum: Ruby Analysing ruby code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2007-05-22 23:45
(Received via mailing list)
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
Rick D. (Guest)
on 2007-05-23 00:07
(Received via mailing list)
On 5/22/07, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> 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 (Guest)
on 2007-05-23 00:20
(Received via mailing list)
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 R. (Guest)
on 2007-05-23 00:26
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
Luis P. (Guest)
on 2007-05-23 00:34
(Received via mailing list)
On 5/22/07, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> 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/ParseTree/

[1] http://rubyforge.org/projects/parsetree/
This topic is locked and can not be replied to.