Need advice about which tool to choose?Ask the StackShare community!
Crystal vs Ruby: What are the differences?
Introduction
Crystal and Ruby are both popular programming languages that share similarities given their heritage from the Ruby programming language. However, there are several key differences that set them apart from each other. This article will delve into the major differences between Crystal and Ruby, highlighting their distinctions and providing a comprehensive understanding of each language.
1. Compilation vs Interpretation:
Crystal is a compiled language, which means that it goes through a separate compilation step before being executed. This allows for better performance and optimization since the code can be transformed into machine-level instructions. In contrast, Ruby is an interpreted language, where the code is executed directly without a prior compilation step. This difference in the execution model leads to varying performance characteristics between the two languages.
2. Static Typing vs Dynamic Typing:
One of the fundamental differences between Crystal and Ruby lies in their typing systems. Crystal is statically typed, meaning that variables are required to have a specific type assigned at compile-time. This enables compile-time type checking and enhances code reliability and performance optimizations. On the other hand, Ruby is dynamically typed, allowing variables to hold values of any type at runtime. While dynamic typing offers flexibility and ease of use, it can introduce potential runtime errors due to type mismatches.
3. Memory Management:
Crystal adopts manual memory management using an automatic reference counting (ARC) system. This means that developers have to explicitly manage memory by allocating and releasing resources. In contrast, Ruby utilizes a garbage collector that automatically manages memory allocation and object deallocation. The garbage collector tracks objects that are no longer in use and frees up the associated memory. This distinction affects memory usage, performance, and the developer experience in terms of memory management.
4. Concurrency and Parallelism:
Another significant difference between Crystal and Ruby is their approaches to concurrency and parallelism. Crystal provides built-in support for lightweight fibers, which can be viewed as cooperative threads, allowing developers to write concurrent code with ease. Additionally, Crystal has a built-in concurrent programming construct called Channel
, which facilitates communication between fibers. Conversely, while Ruby offers concurrency mechanisms like threads and processes, it lacks native support for lightweight fibers and requires using external libraries for achieving concurrent programming.
5. Performance:
Due to its compilation process and static typing, Crystal generally offers superior performance compared to Ruby. The pre-compiled nature of Crystal's code allows the compiler to perform various optimizations, resulting in faster execution. Additionally, Crystal's static typing eliminates the runtime type checks present in Ruby, further enhancing performance. These performance advantages make Crystal a compelling choice for high-performance applications or areas where speed is crucial.
6. Interoperability:
Although Crystal and Ruby share a common syntax, they are not completely compatible with each other. While it is possible to call Crystal code from Ruby and vice versa, there are certain limitations and considerations. Crystal can directly use Ruby libraries through its C binding compatibility. However, due to the differences in their respective type systems and execution models, interoperability between the two languages can be challenging and may require additional work to bridge the gap effectively.
In Summary, Crystal and Ruby differ in terms of their compilation models, typing systems, memory management, concurrency mechanisms, performance, and interoperability. These distinctions make each language suitable for different use cases and development scenarios.
I was considering focusing on learning RoR and looking for a work that uses those techs.
After some investigation, I decided to stay with C# .NET:
It is more requested on job positions (7 to 1 in my personal searches average).
It's been around for longer.
it has better documentation and community.
One of Ruby advantages (its amazing community gems, that allows to quickly build parts of your systems by merely putting together third party components) gets quite complicated to use and maintain in huge applications, where building and reusing your own components may become a better approach.
Rail's front end support is starting to waver.
C# .NET code is far easier to understand, debug and maintain. Although certainly not easier to learn from scratch.
Though Rails has an excellent programming speed, C# tends to get the upper hand in long term projects.
I would avise to stick to rails when building small projects, and switching to C# for more long term ones.
Opinions are welcome!
We have a lot of experience in JavaScript, writing our services in NodeJS allows developers to transition to the back end without any friction, without having to learn a new language. There is also the option to write services in TypeScript, which adds an expressive type layer. The semi-shared ecosystem between front and back end is nice as well, though specifically NodeJS libraries sometimes suffer in quality, compared to other major languages.
As for why we didn't pick the other languages, most of it comes down to "personal preference" and historically grown code bases, but let's do some post-hoc deduction:
Go is a practical choice, reasonably easy to learn, but until we find performance issues with our NodeJS stack, there is simply no reason to switch. The benefits of using NodeJS so far outweigh those of picking Go. This might change in the future.
PHP is a language we're still using in big parts of our system, and are still sometimes writing new code in. Modern PHP has fixed some of its issues, and probably has the fastest development cycle time, but it suffers around modelling complex asynchronous tasks, and (on a personal note) lack of support for writing in a functional style.
We don't use Python, Elixir or Ruby, mostly because of personal preference and for historic reasons.
Rust, though I personally love and use it in my projects, would require us to specifically hire for that, as the learning curve is quite steep. Its web ecosystem is OK by now (see https://www.arewewebyet.org/), but in my opinion, it is still no where near that of the other web languages. In other words, we are not willing to pay the price for playing this innovation card.
Haskell, as with Rust, I personally adore, but is simply too esoteric for us. There are problem domains where it shines, ours is not one of them.
In 2015 as Xelex Digital was paving a new technology path, moving from ASP.NET web services and web applications, we knew that we wanted to move to a more modular decoupled base of applications centered around REST APIs.
To that end we spent several months studying API design patterns and decided to use our own adaptation of CRUD, specifically a SCRUD pattern that elevates query params to a more central role via the Search action.
Once we nailed down the API design pattern it was time to decide what language(s) our new APIs would be built upon. Our team has always been driven by the right tool for the job rather than what we know best. That said, in balancing practicality we chose to focus on 3 options that our team had deep experience with and knew the pros and cons of.
For us it came down to C#, JavaScript, and Ruby. At the time we owned our infrastructure, racks in cages, that were all loaded with Windows. We were also at a point that we were using that infrastructure to it's fullest and could not afford additional servers running Linux. That's a long way of saying we decided against Ruby as it doesn't play nice on Windows.
That left us with two options. We went a very unconventional route for deciding between the two. We built MVP APIs on both. The interfaces were identical and interchangeable. What we found was easily quantifiable differences.
We were able to iterate on our Node based APIs much more rapidly than we were our C# APIs. For us this was owed to the community coupled with the extremely dynamic nature of JS. There were tradeoffs we considered, latency was (acceptably) higher on requests to our Node APIs. No strong types to protect us from ourselves, but we've rarely found that to be an issue.
As such we decided to commit resources to our Node APIs and push it out as the core brain of our new system. We haven't looked back since. It has consistently met our needs, scaling with us, getting better with time as continually pour into and expand our capabilities.
In December we successfully flipped around half a billion monthly API requests from our Ruby on Rails application to some new Python 3 applications. Our Head of Engineering has written a great article as to why we decided to transition from Ruby on Rails to Python 3! Read more about it in the link below.
When I was evaluating languages to write this app in, I considered either Python or JavaScript at the time. I find Ruby very pleasant to read and write, and the Ruby community has built out a wide variety of test tools and approaches, helping e deliver better software faster. Along with Rails, and the Ruby-first Heroku support, this was an easy decision.
Pros of Crystal
- Compiles to efficient native code38
- Ruby inspired syntax36
- Performance oriented - C-like speeds32
- Gem-like packages, called Shards23
- Can call C code using Crystal bindings20
- Super Fast18
- Typed Ruby <318
- Open Source17
- Minimal Runtime14
- Cute11
- Clean code9
- Concurrent9
- Productive9
- Great community4
- Powerful2
- Program compiled into a single binary2
- Simplicity2
- Meta-Programming (via Macros)2
- Feels like duck types, safe like static types2
- Null Safety2
- Type inference2
- Has builtin LLVM support library1
- Statically linked binaries that are simple to deploy1
- Fun to write1
- High-performance1
- Simple, minimal syntax1
- Compile time statically safe macros1
- Concise1
- Statically Safe Monkey Patching1
- Fibers1
- Spawn1
- Meta-programming1
- Productivity1
- Elegant1
- Cross-platform1
Pros of Ruby
- Programme friendly605
- Quick to develop536
- Great community490
- Productivity468
- Simplicity432
- Open source273
- Meta-programming234
- Powerful207
- Blocks156
- Powerful one-liners139
- Flexible69
- Easy to learn58
- Easy to start51
- Maintainability42
- Lambdas37
- Procs30
- Fun to write21
- Diverse web frameworks19
- Reads like English13
- Makes me smarter and happier10
- Rails9
- Very Dynamic8
- Elegant syntax8
- Matz6
- Object Oriented5
- Programmer happiness5
- Elegant code4
- Generally fun but makes you wanna cry sometimes4
- Friendly4
- Fun and useful4
- Easy packaging and modules3
- There are so many ways to make it do what you want3
- Primitive types can be tampered with2
Sign up to add or upvote prosMake informed product decisions
Cons of Crystal
- Small community13
- No windows support3
- No Oracle lib1
Cons of Ruby
- Memory hog7
- Really slow if you're not really careful7
- Nested Blocks can make code unreadable3
- Encouraging imperative programming2
- No type safety, so it requires copious testing1
- Ambiguous Syntax, such as function parentheses1