{"id":19383880,"url":"https://github.com/docspring/ruby_crystal_codemod","last_synced_at":"2025-04-30T04:49:54.513Z","repository":{"id":56893402,"uuid":"225697486","full_name":"DocSpring/ruby_crystal_codemod","owner":"DocSpring","description":"A codemod / transpiler that can help you convert Ruby into Crystal","archived":false,"fork":false,"pushed_at":"2024-05-17T02:11:16.000Z","size":970,"stargazers_count":43,"open_issues_count":2,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-30T04:49:49.298Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DocSpring.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-03T19:20:18.000Z","updated_at":"2024-08-19T00:34:12.000Z","dependencies_parsed_at":"2024-11-02T20:41:19.094Z","dependency_job_id":null,"html_url":"https://github.com/DocSpring/ruby_crystal_codemod","commit_stats":null,"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DocSpring%2Fruby_crystal_codemod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DocSpring%2Fruby_crystal_codemod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DocSpring%2Fruby_crystal_codemod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DocSpring%2Fruby_crystal_codemod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DocSpring","download_url":"https://codeload.github.com/DocSpring/ruby_crystal_codemod/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251644827,"owners_count":21620630,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T09:28:14.995Z","updated_at":"2025-04-30T04:49:54.497Z","avatar_url":"https://github.com/DocSpring.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![CircleCI](https://circleci.com/gh/DocSpring/ruby_crystal_codemod.svg?style=svg)](https://circleci.com/gh/DocSpring/ruby_crystal_codemod)\n\n# Ruby =\u003e Crystal Codemod\n\nThis project is a fork of [Rufo](https://github.com/ruby-formatter/rufo). (Rufo and Crystal were both created by [Ary Borenszweig](https://github.com/asterite)!)\n\n\u003e Rufo is as an _opinionated_ ruby formatter, intended to be used via the command line as a text-editor plugin, to autoformat files on save or on demand.\n\nThe formatting rules have been modified in an attempt to produce some semi-valid Crystal code. Then you need to add some type annotations and fix any other issues manually. See the [Crystal for Rubyists](https://github.com/crystal-lang/crystal/wiki/Crystal-for-Rubyists) wiki page to learn more about the syntax differences.\n\n\u003e Ruby =\u003e Crystal Codemod / Rufo supports all Ruby versions \u003e= 2.4.5, due to a bug in Ruby's Ripper parser.\n\n## Requirements\n\n* Ruby \u003e= `2.4.5`\n* [Crystal](https://crystal-lang.org/install/) \u003e= `0.31.1`\n\n## Installation\n\nInstall the gem with:\n\n```\n$ gem install ruby_crystal_codemod\n```\n\n## Usage\n\nGo to the directory where you want to convert Ruby files into Crystal. Then run:\n\n```\nruby_crystal_codemod .\n```\n\nThis command will create new `*.cr` files and attempt to fix any simple errors. Then it will\nrun `crystal tool format` to format the generated code.\n\n## Next Steps:\n\n* Once you've fixed all of the syntax and type errors, run `crystal tool format` to autoformat your code.\n* Run [Ameba](https://github.com/crystal-ameba/ameba) for static code analysis (similar to RuboCop), and fix all of the errors.\n  * *(Unfortunately Ameba doesn't have a --fix option yet.)*\n\n## Writing Ruby / Crystal in the same file\n\nIf you want to write both Ruby and Crystal in a Ruby file, you can use some\nspecial `#~# BEGIN \u003clanguage\u003e` and `#~# END \u003clanguage\u003e` comments.\nCode between `#~# BEGIN ruby` and `#~# END ruby` should be uncommented,\nand code between `#~# BEGIN crystal` and `#~# END crystal` should be commented.\nWhen transpiling a Ruby file into Crystal, the transpiler will remove all of the Ruby lines between these comments, and it will uncomment all of the Crystal lines.\n\nThe `BEGIN` / `END` comments can start with either `#~#` or `# ~#`. (Code formatters / linters often enforce a space after the `#` character for comments.)\n\nFor example, here's how you can define a class that works for both Ruby and Crystal:\n(Crystal requires type annotations here.)\n\n```\nclass Foo\n  attr_accessor :foo\n\n  #~# BEGIN ruby\n  def initialize(foo)\n    @foo = foo\n  end\n  #~# END ruby\n  #~# BEGIN crystal\n  # @foo : Int32\n  # def initialize(@foo : Int32); end\n  #~# END crystal\nend\n```\n\nWhen this file is executed by Ruby, Ruby will ignore all of the commented lines. When the file is run through the Ruby =\u003e Crystal transpiler, it will be transformed into the following Crystal code:\n\n```\nclass Foo\n  property :foo\n\n  @foo : Int32\n  def initialize(@foo : Int32); end\nend\n```\n\n(The transpiler automatically renames `attr_accessor` to `property`.)\n\n\u003e See [`spec/fixtures/crystal_codemod_test/example.rb:87`](https://github.com/DocSpring/ruby_crystal_codemod/blob/master/spec/fixtures/crystal_codemod_test/example.rb#L87-L114) for a real-world example that is used in our acceptance specs.\n\n## Status\n\n- [x] Rename all file extensions from `.rb` to `.cr`\n- [x] Replace single quoted strings with double quotes\n- [x] `require_relative \"foo\"` -\u003e `require \"./foo\"`\n- [x] `$:`, `LOAD_PATH` =\u003e Show error and link to docs about CRYSTAL_PATH (for compiler)\n- [x] Translate methods / keywords / operators:\n  - [x] `include?` -\u003e `includes?`\n  - [x] `key?` -\u003e `has_key?`\n  - [x] `detect` -\u003e `find`\n  - [x] `collect` -\u003e `map`\n  - [x] `respond_to?` -\u003e `responds_to?`\n  - [x] `length`, `count` -\u003e `size`\n  - [x] `__dir__` -\u003e `__DIR__`\n  - [x] `and` -\u003e `\u0026\u0026`\n  - [x] `or` -\u003e `||`\n  - [x] `not` -\u003e `!`\n  - [x] `foo.each(\u0026:method)` -\u003e `foo.each(\u0026.method)`\n  - [x] `foo.map \u0026:method` -\u003e `foo.map \u0026.method`\n- [x] `attr_accessor` =\u003e `property`\n- [x] `attr_reader` =\u003e `getter`\n- [x] `attr_writer` =\u003e `setter`\n- [ ] `private` / `protected` methods\n- [ ] `class \u003c\u003c self` =\u003e `def self.foo` (?)\n- [ ] `YAML.load_file(\"./foo.yml\")` =\u003e `YAML.parse(File.read(\"./foo.yml\"))`\n- [ ] .each returns nil - Try to warn if it looks like the return value of `.each` is being used\n- [ ] for loops - Show a warning or link to the docs\n- [ ] Consistent dot notation - `File::exists?` =\u003e `File.exists?`\n\n## Future\n\n* Sorbet Type Annotations -\u003e Crystal type annotations\n  * Integration with [gelauto](https://github.com/camertron/gelauto), to automatically annotate Ruby code with Sorbet type definitions.\n\n## Testing\n\nRun `rspec` to run all the specs and integration tests. I've kept all of the original rufo specs, because they're all really fast, it doesn't hurt to produce nicely formatted Crystal code (before the crystal format pass.)\n\nCrystal-specific formatting specs can be found in `spec/lib/ruby_crystal_codemod/formatter_crystal_specs/*`.\n\nThere's also a Crystal acceptance spec at `spec/lib/ruby_crystal_codemod/crystal_codemod_acceptance_spec.rb`.\nThis transpiles the example Ruby code in `spec/fixtures/crystal_codemod_test`, and makes sure that Ruby\nand Crystal produce the same output when they both run the respective code.\n\n## Developing\n\nBefore submitting a PR, please run:\n\n* `bundle exec rake rubocop -a`\n* `bundle exec rufo lib/ spec/lib/`\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/DocSpring/ruby_crystal_codemod.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdocspring%2Fruby_crystal_codemod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdocspring%2Fruby_crystal_codemod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdocspring%2Fruby_crystal_codemod/lists"}