DEV Community

Ahmad khattab
Ahmad khattab

Posted on

3 2

Discoveries in Ruby(and Rails): Case equality operator

Ruby provides mechanisms for controlling the control-flow of a program. They consist of the if-statement families, if, elsif, else, and unless. Along with case/when.

“The basic idea of the case/when structure is that you take an object and cascade through a series of tests for a match, taking action based on the test that succeeds.”. Black, D. A., & Leo, J. (2019). The well-grounded Rubyist. Manning.

A typical case statement looks like this

tool = "google"

case tool
when "google"
   puts "tool is google"
when "basecamp"
   puts "tool is basecamp"
else
  puts "unknown tool"
end
Enter fullscreen mode Exit fullscreen mode

Every ruby object has a case equality method, called ===, triple equal sign, sometimes called as case subsumption or threequal operator

instead of doing when tool.=== "google". The when "google" keyword provides a syntactical sugar over the previous approach.

The threeequal operator is defined everywhere on every built-in class. You can also implement in classes that you define.

Let's define a Car class.

class Car
  attr_accessor :brand, :type
  def initialize(brand, type)
    self.brand = brand
    self.type = type
  end

  def ===(other_car)                               
    self.brand == other_car.brand
  end
end
Enter fullscreen mode Exit fullscreen mode

Now, we can use them along with case/when.

ford = Car.new "Ford", "F-150"
jeep = Car.new "Jeep", "Wrangler"
tesla_model_x = Car.new "Tesla", "Model-X"
tesla_model_y = Car.new "Tesla", "Model-Y"

case tesla_model_x
when ford
    puts "It's a ford"
when jeep
    puts "It's a Jeep"
when tesla_model_y
    puts "Both are Tesla made"
else
    puts "unknown manufacturer"
end
Enter fullscreen mode Exit fullscreen mode

running this through a ruby session, we get the following

Both are Tesla made
=> nil
Enter fullscreen mode Exit fullscreen mode

Underneath, the ruby interpreter makes a call to the === method on the object, which we defined inside the Car class

  def ===(other_car)                               
    self.brand == other_car.brand
  end
Enter fullscreen mode Exit fullscreen mode

With tripleequal sign operator or case subsumption operator you can allow your classes to work elegently with Ruby's case/when control-flow operators.

Thanks for reading, and Happy Coding.

Resources

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️