Oh that’s awesome, I didn’t realize that!
I’m also interested in converting Crystal syntax to Ruby, and I posted about that here. My idea was just so that I could run some simple Crystal code using Ruby’s interpreter, so I would have access to a real REPL where I can modify variables and define new methods.
But in this case I’m trying to see if I can go the other direction and transpile some Ruby code into the Crystal syntax. I think this could make it easier to port some simple Ruby libraries.
A semantic pass would be really great, but I don’t know if that would be feasible in a Ruby codebase without any type annotations. But I could probably do a simple regex search to see if any of the source files contains def detect
, and then show a big warning in the output.
I have a really simple proof-of-concept working now! https://github.com/DocSpring/ruby_crystal_codemod/tree/master/example_code
It just handles the following cases:
- Rename all file extensions from
.rb
to.cr
- Replace single quoted strings with double quotes (default in rufo)
-
require_relative "foo"
->require "./foo"
-
__dir__
->__DIR__
And now Ruby and Crystal can both run the code and produce exactly the same output.
Haha but my implementation is extremely bad. I kept trying to find some way to reach into the next tokens and modify them, but I couldn’t get anything to work. So I just resorted to this crazy @wait_for_consume_token_count_then_set_relative_path
counter to get something working.
Is there any easy way to update the path string in #visit_command
and add ./
?