Would one be able to use Ruby gems with Crystal?
Asked Answered
F

1

6

Developers say that Crystal follows Ruby language syntax. So can I (or would I, in the future) just require a Ruby gem and it magically builds and properly working and so on?

Fletcherfletcherism answered 22/2, 2016 at 20:56 Comment(1)
Maybe some kind of built-in converter?Fletcherfletcherism
Z
17

No.

The language evolved a lot and differs significantly from Ruby these days. While it feels a bit like Ruby, if you actually try it you'll quickly understand why that question doesn't even come up except for the most simple gems you can imagine. Just two examples:

Crystal has no single quoted string literals:

'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 can't 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

You can read more for example here or here.

Zo answered 23/2, 2016 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.