Getting Cozy with Ruby

Ben Lovy - Mar 7 '20 - - Dev Community

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.

Testing

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:

$ gem install minitest
Enter fullscreen mode Exit fullscreen mode

Then, in my_math_test.rb:

require 'minitest/autorun'
require_relative 'my_math'

# MyMath sanity tests
class MyMathTest < Minitest::Test
  def test_times_two
    # skip
    assert_equal 8, MyMath.my_times_two(4)
  end
end
Enter fullscreen mode Exit fullscreen mode

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:

describe MyMath do
   it "multiplies 4 by 2" do
     math = MyMath.new
     expect(math.my_times_two(4)).to eq(8)
  end
end
Enter fullscreen mode Exit fullscreen mode

This reminds me of using Mocha with Chai in JavaScript. There's an intro guide here.

%w

Don't do this:

JOES = ["average", "DiMaggio", "morning"]
Enter fullscreen mode Exit fullscreen mode

Do this:

JOES = %w[average DiMaggio morning]
Enter fullscreen mode Exit fullscreen mode

Whoa! Also, %i works for symbols:

SYMS = %i[one two three]
# [:one, :two, :three]
Enter fullscreen mode Exit fullscreen mode

Object.freeze

Not planning to ever change your JOES constant? Tell Ruby that you mean it and freeze 'em:

JOES = %w[average DiMaggio morning].freeze
Enter fullscreen mode Exit fullscreen mode

Now it's actually immutable! Read a lot more about Ruby constants here.

String interpolation

Basically, it does it. This works:

def say_name(name)
  output = "Hello, "
  output << name
  output << "!"
  puts output
end
Enter fullscreen mode Exit fullscreen mode

This is better:

def say_name(name)
  puts "Hello, " + name + "!"
end
Enter fullscreen mode Exit fullscreen mode

But you probably want this:

def say_name(name)
  puts "Hello, #{name}!"
end
Enter fullscreen mode Exit fullscreen mode

class << self

To redefine a method on self, so you can call MyClass.my_method, you can define it on self explicitly:

class MyClass
  def self.my_method(str)
    puts str
  end
end
Enter fullscreen mode Exit fullscreen mode

If you're doing this a bunch, you can open up the eigenclass, or singleton class, directly:

class MyClass
  class << self
    def my_method(str)
      puts str
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

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:

class MyClass
end

def MyClass.my_method(str)
  puts str
end
Enter fullscreen mode Exit fullscreen mode

This class << self is also the best way to make a private method:

class MyClass
  class << self
    private

    def my_private_method(str)
      puts str
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Otherwise, you have to use private_class_method which I think looks gross:

class MyClass
  private_class_method def self.my_private_method(str)
    puts str
  end
end
Enter fullscreen mode Exit fullscreen mode

attr_accessor

You can publicly expose instance variables directly:

class MyClass
  def initialize
    @value = 0
  end
  def show_value
    puts @value
  end
end
Enter fullscreen mode Exit fullscreen mode

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:

class MyClass
  def initialize
    @value = 0
  end

  def value
    @value
  end
  def value=(new_value)
    @value = new_value
  end

  def show_value
    puts value # don't point to the `@value` var, send the `value` message
  end
end
Enter fullscreen mode Exit fullscreen mode

It's usually better to create both at once with attr_accessor:

class MyClass
  attr_accessor :value

  def initialize
    @value = 0
  end

  def show_value
    puts value
  end
end
Enter fullscreen mode Exit fullscreen mode

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.

Structs

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:

class MyRect
  attr_reader :rect_dims

  def initialize(arr)
    @rect_dims = arr
  end
  def show_size
    puts "Size: #{rect_dims[0]}x#{rect_dims[1]}"
  end
end
Enter fullscreen mode Exit fullscreen mode

Do this:

class MyRect
  attr_reader :rect_dims
  def initialize(arr)
    @rect_dims = make_dim_struct(arr)
  end
  def show_size
    puts "Size: #{rect_dims.width}x#{rect_dims.height}"
  end

  RectDims = Struct.new(:width, :height)
  def make_dim_struct(arr)
    RectDims.new(arr[0], arr[1])
  end
end
Enter fullscreen mode Exit fullscreen mode

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.

rubocop

Rubocop is a linter that adheres to the Ruby Style Guide. Do this:

$ gem install rubocop
Enter fullscreen mode Exit fullscreen mode

Then always do this:

$ rubocop myFile.rb
Enter fullscreen mode Exit fullscreen mode

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 with the code from this post:

To be continued...

There's actually a bunch of cool stuff out there. Methods like gsub and inject stand out as some pleasant surprises from my first explorations. This comment from @ben has been spot on:

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .