Getting Cozy with Ruby
When I first approached Ruby, I basically looked at it like “dynamic C++”, because that was the best analogue I had. Of course, that has required some tweaking. This post talks about some Ruby-specific idioms that aren’t directly related to concepts I knew, and assumes you already use another class-based OOP language such as C++, Java, or Python.
Now that I’ve been using Ruby for about three days, I’m obviously a complete expert and to be implicitly trusted (heh). If you do see something wrong, please correct me!
Huge thanks to @dvik1950 on the Ruby exercism mentored track for many of these.
I know this splash image only mildly applies but it was too cool not to use. You can’t change my mind.
This is a dynamic language. I am so very much a static kinda person, so unit testing is pretty much the first priority for not losing hair/sleep.
I found the easiest to start with is minitest:
Then, in my_math_test.rb:
Uncomment skip to skip the test, which avoids commenting/uncommenting the whole function. Also, the MyMathTest < Minitest::Test syntax is how you define a subclass, so MyMathTest inherits from Minitest::Test.
For a much more involved solution, there’s rspec which provides a testing DSL instead of using Ruby functions. Here’s what that test might look like:
Don’t do this:
Whoa! Also, %i works for symbols:
Not planning to ever change your JOES constant? Tell Ruby that you mean it and freeze ’em:
Now it’s actually immutable! Read a lot more about Ruby constants here.
Basically, it does it. This works:
This is better:
But you probably want this:
To redefine a method on self, so you can call MyClass.my_method, you can define it on self explicitly:
If you’re doing this a bunch, you can open up the eigenclass, or singleton class, directly:
I think that’s a little less noisy, even at the cost of some extra lines and indentation. If you want to go super concise, you can just dot-operator your way all the way in:
This class << self is also the best way to make a private method:
Otherwise, you have to use private_class_method which I think looks gross:
You can publicly expose instance variables directly:
You define the constructor with initialize().
In Ruby, however, it’s extremely easy to create getters and setters and usually preferable. You can manually do so:
It’s usually better to create both at once with attr_accessor:
You can also use attr_reader or attr_writer for just the getter or just the setter, respectively.
The benefit is that now if this logic needs to change, all you need to do is define that method, and every call site automatically reflects the new logic.
You don’t have to use nested arrays and whatnot for complex data just because we’re using a dynamic language. Ruby provides a Struct class for structured data, which gives you these accessors methods automatically
This is a contrived example, but instead of this:
As before, the whole point is to localize the definition of your data’s structure to one single place, should it need to change again.
Then always do this:
Fix everything it says to fix, and if you don’t understand what it’s saying or why, look it up. Learning!
Here’s a repl.it link with the code from this post.
Ruby is a scripting language at its core. One command after another. Almost brutally simplistic. I’d recommend opening up IRB and typing some commands. It’s actually pretty close to coding in the environment. Ruby is sort of object oriented, sort of functional, and has a few ways to do the same thing most of the time. It’s a bit of a free-for-all.
There really is a bit of everything here. It’s got a bit of pretty much every language I’ve previously used! Nailing down the clearest solution or most correctly applied pattern is a little tricker.
I looked at Ruby a long time ago, as one of my very first forays into programming. I liked how concise it was, and why’s poignant guide is a really fun read - if you haven’t read it, you should at least give it a try even if you’re not necessarily targetting Ruby just for how unique it is.
However, I’m having a lot more fun with the language returning again as a more experienced coder. I think having the context I’ve built using a variety of different types of languages has helped me understand how to apply Ruby effectively. It was difficult for me to learn how to use Ruby at the same time as learning how to program in a general sense, despite how easy it is to get up and running.
I don’t know that I’d personally recommend Ruby as a first programming language. There are so many paradigms available with so little friction. Do you agree or disagree?
Photo by Road Trip with Raj on Unsplash