The language Series: Ruby


There are only two kinds of languages: the ones people complain about and the ones nobody uses.

Bjarne Stroustrup

I used to write a humorous series about programming languages about a decade ago. In this post, I continue that tradition with Ruby. 

Learning Ruby

Whenever I start a new role, I prefer to get a broad idea about the programming languages and tools used there. This helps me to familiarize myself with the work environment.  I chose Ruby for two reasons- to contribute more to open source and to become more effective. I am not expected to code, but I strive to review pull requests and understand the architecture. 

Ruby’s syntax felt unfamiliar to me initially, and I found myself perplexed by some of the PRs I encountered. I attempted to learn by watching Pluralsight videos but quickly realized I needed more than passive learning.

Discovering Exercism was the game changer. It helped me stay on track and, most importantly, forced me to practice and actively apply my learning. This approach perfectly suits my learning style: I consistently aim for a Pomodoro of Ruby daily practice. The strict time constraints help me stay focused, and completing exercises provides visual feedback that motivates me to continue.

 The Good Parts

  • Excellent readability: Ruby sometimes reads like English (which reminds me of Python). 
  • Intuitiveness: The principle of least surprise is so powerful – you have a high chance of correctly guessing method names and helpers. Chances are high that there is already inbuilt support for something you want to do; this rich library and sensibility in naming conventions lowers the learning curve significantly. For example, Arrays override the + and - operators for concatenation and set difference1.
  • Balance: Ruby dynamic typing enables rapid prototyping while its strongly typed nature introduces appropriate guardrails to prevent newbie mistakes. 
  • REPL FTW: I love, love languages with REPL support. A REPL is handy when writing a script or solving a little problem.
  • Batteries included: Ruby has a rich standard library, a vibrant community, and multiple powerful libraries. Operations like flatten, uniq, transposeupto etc, already exist in the native language (unlike JavaScript or C, where you must bring your batteries).
  • Terseness: It allows you to chain methods and encourages a terse style that drops parentheses. 
  • Some cool, supported operations in Ruby:
    • Swapping: x, y = y, x
    • Iterating through two lists simultaneously using the zip keyword.
    • Ranges are dope! The .. and ... operators allow you to do 'a'..'z' and 1..3.
    • Allows returning multiple values from a method.

The Bad Parts

All languages have flaws, and Ruby is no exception.

  • There are 1 bazillion ways to do anything – having multiple redundant methods can be overwhelming. Sometimes, less is more.
  • Popularity: Ruby is less popular than it was a decade ago thus, it might be challenging to land a job at a Ruby shop. It is a language slowly falling out of usage2.
  • Ruby doesn’t support first-class functions and closures mask scope. That does restrict some elegant programming solutions. 
  • Floating Point: .3/.1 = 2.99999999999999996 and 0.1 + 0.2 !== 0.3; the result is 0.30000000000000004; to be honest, this is the floating point challenge, and few languages support this innately.
  • Unintended Consequences: Ruby’s expressiveness can be a double-edged sword; it’s easy to fall into some gotchas since some methods might not behave as expected. For example:
    • 1000000000000000000000.equal? 1000000000000000000000 is false
    • 1000000000000000000000.eql? 1000000000000000000000 is true
    • 1000000000000000000000 == 1000000000000000000000 is true
    • 1000000000000000000000 === 1000000000000000000000 is true
  • Upgrading Ruby and irb can be a pain.

Don’t miss the next post!

Subscribe to get regular posts on leadership methodologies for high-impact outcomes.

Join 3,994 other subscribers

The Weird Parts

  • The return statement is optional; it feels odd, but it makes sense once you get the hang of it.
  • Constants are declared by naming variables in UPPERCASE 
  • @ for instance variables and using self for static methods?
  • module and classes are somewhat interchangeable.
  • Bang (!) methods will modify state.
  • The map operator doesn’t pass the index; you have to use a decorator
  • Everything is an object, even primitives!
  • raise instead of throw for exceptions
  • The syntax allows leaving out braces; powerful for chaining since everything is an object but can be hard to read and grok for newbies.
  • &. for null checks; I prefer C#’s ?.

Good for beginners

Java is a popular choice for those starting out in programming. It’s often the first language that people learn for object-oriented programming (OOP). However, due to it being a compiled language, it can be challenging to interact with programs quickly, which is essential during the learning process.

If you’re interested in programming as a hobby, I would suggest starting with either Python or Ruby. It’s interesting to note that these languages aren’t as commonly taught in schools. However, their intuitive approach and ease of learning may make it simpler for beginners to concentrate on the basics of programming. Once you have a strong understanding of the fundamentals, you can then move on to learning other languages.

Tips for Starting with Ruby

Go for it! Dive into the language, learn the Rubyisms, and gradually, you’ll begin to appreciate its beauty. If you’re used to languages that use a lot of braces and parentheses (like Java), Ruby’s unique syntax might be confusing at first. However, once you become familiar with it, you’ll wonder how you ever got anything done in other languages. Ruby’s expressiveness is incredible – you can write little code but achieve so much.

Conclusion

Really enjoyed writing this since it’s been a long time since I wrote about languages. Here is a short summary of some of my favorite features across multiple languages:

  • Python’s list comprehension and three-way comparison (e.g., if 3 > x > 1)
  • JavaScript’s lambdas
  • Ruby’s ranges
  • C# linq
  • Scheme’s reliance on lists
  • C’s small grammar 

Do you like this post? Check out my earlier posts on CPythonJavaScript, PHP, and Java.

Don’t miss the next post!

Subscribe to get regular posts on leadership methodologies for high-impact outcomes.

Join 3,994 other subscribers
  1. I wish more languages would override such primitives for ease of use and expressiveness. Python is similar, too, but it doesn’t support set differences with “-“ natively. ↩︎
  2. Ranks 19 out of 20 on the TIOBE index. ↩︎

Discover more from CodeKraft

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.