← Back to context

Comment by exabrial

10 hours ago

Worth mentioning is Crystal lang: Ruby, with types!

Yeah, I think it would be great if this project made it clear how this compares with Crystal in terms of the syntax.

  • In terms of syntax, they are incompatible. At the very least, crystal require more whitespace than what the examples show.

How alike actually are Ruby and Crystal? I’ve heard the similarity is only skin-deep: similar syntax but quite different semantics.

In other words, isn’t describing Crystal as “Ruby with types” similar to describing C++ as “JavaScript with types”?

  • As others have mentioned, Crystal is close to Ruby in many ways, such that some simpler code will port straight over. I've managed to port a large Ruby application (the sup email client) to Crystal, and a lot of the code just worked, but I still had tweak just about everything else to get it to compile. The hardest bits were the places that used Ruby's dynamic nature, e.g., constructing method names at runtime and then calling them with send, or creating methods on the fly, or data structures that mixed up types freely.

    Crystal's intent, as I see it, is very different from Ruby's. Because it compiles down to machine code in a single executable, it's good for making things that are fast and easy to deploy. I've used it to make small web services as well as the bigger thing I mentioned above.

  • Well they're similar enough for this project to work:

    https://github.com/wouterken/crystalruby

    It allows you to call crystallize on a ruby method and then have it recompile with crystal and called over FFI, which is pretty neat.

    It would be really cool if this trb syntax could get close to the crystal syntax for method signatures at least.

    I'd love to be able to move chucks of hot code to Crystal but leave everything else in ruby for compatibility with existing projects.

  • It's different, but close enough to not matter a lot of the time. As in, some constructs are not allowed but they're extremely rare in practice and have simple workarounds, even if you don't preserve the exact same usage syntax.

    So, you extremely rarely can run Ruby code in Crystal. But simple scripts are trivial to annotate. Larger apps won't require huge changes, but you're likely to run into dependencies you also need to port.

  • Semantics are quite different, but the stdlib APIs are similar enough that it really feels rubylike, unless you want to use some of that dynamism in ruby. The code ends up looking a lot more similar than c++ compared to JavaScript.

  • Yes, Crystal is not a superset of Ruby with all of its behaviors and semantics. And thus, it is great if you are starting fresh and don't mind foregoing the Ruby gem ecosystem and the popular frameworks like Rails proper.

    But it's not a migration path for Ruby codebases to add types, like the new initiatives for type annotations are. It's just that the syntax used by the available options has been somewhere between bad and downright terrifying until now.