Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would one be able to use Ruby gems with Crystal?

Tags:

crystal-lang

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?

like image 365
Vlad Faust Avatar asked Feb 22 '16 20:02

Vlad Faust


People also ask

Is Crystal better than Ruby?

Developers describe Crystal as "Fast as C, slick as Ruby". Crystal is a programming language that resembles Ruby but compiles to native code and tries to be much more efficient, at the cost of disallowing certain dynamic aspects of Ruby.

What type of gem is Ruby?

Ruby gemstones are a variety of the mineral corundum. More specifically, they are the red variety, which is colored by chromium. Other colors of corundum are classified as sapphire. The word “ruby” comes from the Latin word “ruber” for the red hue.


1 Answers

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.

like image 95
Jonne Haß Avatar answered Nov 07 '22 03:11

Jonne Haß