Can Ruby stones be used with Crystal?

Developers say Crystal follows Ruby syntax. Can I (or will in the future) just require a Ruby stone, and it magically builds and works correctly, etc.

+4
source share
1 answer

No.

The language has evolved a lot and is significantly different from Ruby these days. Although it is a little like Ruby, if you actually try it, you will quickly understand why this question does not even arise, with the exception of the simplest stones you can imagine. Just two examples:

There are no single quoted string literals in the crystal:

'c'        # Ok in Ruby and Crystal, but different things,
           # a String in Ruby, a Char in Crystal

"a string" # Ok in Ruby and Crystal, a String in both

'a string' # Ok in Ruby, but a compile time error in
           # Crystal, since character literals are for a single character

Crystal cannot infer the type of empty arrays or hashes:

["foo"]                # Ok in Ruby and Crystal, an Array in Ruby,
                       # an Array(String) in Crystal

{"foo" => "bar"}       # Ok in Ruby and Crystal, a Hash
                       # in Ruby, a Hash(String, String) in Crystal

[]                     # Ok in Ruby, but a compile time error in Crystal
[] of String           # Ok in Crystal, but a syntax error in Ruby
{}                     # Ok in Ruby, but a compile time error in Crystal
{} of String => String # Ok in Crystal, but a syntax error in Ruby

.

+12

Source: https://habr.com/ru/post/1629953/


All Articles