How to parse ruby files programmatically

(Andrey Tuzhilin) #1

Hi!

I need to parse ruby files (files, containing ruby code) and edit them. Specifically, I need:

1. Extract the list of classes defined in file
2. Without them being applied in current env (if some file for instance contains `String` class method patching, I don’t want it to be patched in current ruby session)

Is there a way to accomplish this?

(Gerald Bauer) #2

Hello,

    You might just use some text patterns / regular expressions :-).

    For full-fledged parsers, see the Ruby Parser / Unparser section
in the Awesome Rubies page for some options.

    Happy coding with ruby. Cheers. Prost.

[1] https://github.com/planetruby/awesome-rubies#ruby-parser--unparser

(Xavier Noria) #3

Depending on how generic is the input, that might be hard, think
const_set(name, Class.new), eval STRING, etc.

···

On Tue, 7 May 2019 at 14:20, Gerald Bauer <gerald.bauer@gmail.com> wrote:

Hello,

    You might just use some text patterns / regular expressions :-).

    For full-fledged parsers, see the Ruby Parser / Unparser section
in the Awesome Rubies page for some options.

    Happy coding with ruby. Cheers. Prost.

[1] https://github.com/planetruby/awesome-rubies#ruby-parser--unparser

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--
Sent from Gmail Mobile

(Paul McKibbin) #4

Hi,

Have you taken a look at RubyParser, SexpProcessor and Ruby2Ruby from the seattlerb. Using these should be a lot easier than trying to generate it with regular expressions.

Best wishes,
Paul

···

On 7 May 2019, at 12:54, Andrey Tuzhilin <andrei.tuzhilin@gmail.com> wrote:

Hi!

I need to parse ruby files (files, containing ruby code) and edit them. Specifically, I need:

1. Extract the list of classes defined in file
2. Without them being applied in current env (if some file for instance contains `String` class method patching, I don’t want it to be patched in current ruby session)

Is there a way to accomplish this?

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>