Skip to content

Commit

Permalink
Porting here document
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Feb 21, 2023
1 parent e7857ff commit f717243
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ The only dependency is the ruby builtin regex engine, MRI 2.0+ should work.

2. renamed the extension `.rb` to `.cr` for files will in-place use this script.

3. run bin/port_ruby_to_crystal, this will do replace on known rules. if it give some error messages,
just fix it manually, until no any error.
3. run bin/port_ruby_to_crystal, this will do in replace all "*.cr" use known rules.
You bet added those files into git before do it to track changes.

4. if it give message like `syntax error in '???': ...`, this may be a bug of the script
making the wrong substitution, Or it's just not covered by the rules.
for this case, you need fix it manually, until no any error.

```sh
$ cd you_project_root
$: path/to/port_ruby_to_crystal
$: cd you_project_root
$: port_ruby_to_crystal
```

This project can also be used as a learning resource to see exactly what Ruby APIs are different with Crystal, just check the diff of `./test/test.rb` with `./test/test.cr`.

## Why not replace `[]` to `[] of SomeType`, `{}` to `{} of SomeType => SomeType`, it's really annoying of those syntax error.

There is no good default SomeType for this case, following is a good [advice](https://forum.crystal-lang.org/t/is-there-exists-a-dart-dynamic-type-equivalent-for-crystal-or-equivalent-usage/5082/8?u=zw963)
There is no good default SomeType for this case, following is a good [advice](https://forum.crystal-lang.org/t/is-there-exists-a-dart-dynamic-type-equivalent-for-crystal-or-equivalent-usage/5082/8?u=zw963), you can replace all unkown type to `BlackHole`, make formatter check pass, then figure the correct type out.

# Contributing

Expand Down
2 changes: 2 additions & 0 deletions bin/port_ruby_to_crystal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def replace(code)
code.gsub!(/\b\.strftime(?:\(|\s+)(?:"|')(.+?)(?:"|')\)?/, '.to_s("\1")')
code.gsub!(/\.delete_suffix\b/, '.rstrip')
code.gsub!(/\.delete_prefix\b/, '.lstrip')
# code.gsub!(/\.freeze\b/, '')
code.gsub!(/\$stdin\b/, '\1STDIN')
code.gsub!(/\$stdout\b/, '\1STDOUT')
code.gsub!(/\$stderr\b/, '\1STDERR')
Expand All @@ -41,6 +42,7 @@ def replace(code)
# so replace rm_f into rm_rf for now
code.gsub!(/\bFileUtils.rm_f\b/, 'FileUtils.rm_rf')
code.gsub!(/\balias\s+([\w?!]+)\s+([\w?!]+)\b/, 'def \1; \2; end')
code.gsub!(/<<~([A-Z_]+)/, '<<-\1')
end

if ARGV[0] == '--test'
Expand Down
6 changes: 6 additions & 0 deletions test/test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ def aaa
end
def bbb; aaa; end
bbb

x = <<-XML
<parent>
<child />
</parent>
XML
6 changes: 6 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ def aaa
end
alias bbb aaa
bbb

x = <<~XML
<parent>
<child />
</parent>
XML

0 comments on commit f717243

Please sign in to comment.