2

I want to edit Ruby files using Python scripts and I need to

  1. Check if a function call exists within a class, add if not present, check function call args if present and change accordingly
  2. Check if a function def exists and if not insert a function definition inside a class
  3. Points 1 and 2 for blocks (and also for classes and modules)
  4. When rebuilding the edited file comments should be intact

Suggest me a Ruby/Rails parser written in Python. Also, tell me if it is a lot easier in Ruby itself rather than finding a library in Python, and if that would give me a lot more flexibility and even more abilities to control the Ruby files.

1

Are you trying to edit the ruby files while the ruby application is running?
Or are you using the python scripts to generate a project, like cookiecutter?

Ruby does support reflection and metaprogramming so you can:

  1. check if a method exists using respond_to?
  2. define a method using define_method.

If it were me, I'd do everything in ruby, but it depends on your usecase.

| improve this answer | |
  • I am trying to generate a project just like cookiecutter. And you are right, I later realized I will have to end up having a full fledge ruby parser in one way or the other written wholly in Python, and it makes more sense to use Ruby's own reflection and meta api to change things in ruby files for checking if the function exists or not, or to add new ones. But it would still fall short when it comes to adding an assignment statement and function calls in an existing function or blocks. (Or would it? do we have methods for that as well?) – Saravanabalagi Ramachandran Nov 14 '18 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.