🤖

Engineering

Mar 2026

⏱ 8 phút phút đọc

Tản mạn một chút về case ... when với ngôn ngữ lập trình Ruby

V

Nguyễn Văn Minh — CTO / AI Lead

Vareal Vietnam

As we all know, is one of the very popular syntax when programming with the Ruby language.case...when

However, the use of this syntax is very diverse and it is easy to make you feel confused when you do not understand the essence and how it works.

In this article, we will briefly share the nature of the operation of the case… when, as well as ways to apply it to be able to take advantage of this syntax in your own way.

1. Common use cases include:

1.1. Full Type

case grade
when 'A'
  p 'Good'
when 'B'
  p 'Good'
when 'C'
  p 'OK'
else
  p 'Try harder'
end

1.2. Lack of value

case
when weather == 'rain'
  p 'I love that'
when bank_amount > 99999999999999999999999999999999999
  p "I'm rich"
else
  p 'Hmmm'
end

1.3. Dạng multiple-value

case grade
when 'A', 'B'
  p 'Good'
when 'C'
  p 'OK'
else
  p 'Try harder'
end

1.4. Special forms

Let’s start with more fancy forms!

case 'hello'
when /ello/
  p 'Oh hi'
when /\\d*/
  p 'I am sorry'
end
object = Staff.new ....

case object
when Customer
  p 'Hello customer'
when Staff
  p 'Hello staff'
else
  p 'Hello'
end
case 4
when 1..5
  p 'Low'
when 6..10
  p 'Good'
else
  p 'Hmmm...'
end

2. The nature of how it works

If you look at the examples in section 1.4, you will see that the normal comparison is not used.case...when

The reason for this is because the case… when will use case-equality (Used by operator ===).

More simply, you can imagine the first example would be understood as:

case grade
if 'A' === grade
  p 'Good'
elsif 'B' === grade
  p 'Good'
elsif 'C' === grade
  p 'OK'
else
  p 'Try harder'
end

What does case-equality mean?

In fact, depending on the type of object, there will be different implementations, but most of them will be implemented in the following sense:

For example, it can be simply understood as “If a is a set, is b a member of it”. a === b

Specifically:

(1..5) === 2 #true
(1..5) === 3 #true
(1..5) === 6 #false
Integer === 100 #true
Integer === 'hello' #false
/ello/ === 'hello' #true

Once you have understood the operation using to test the conditions, it will shed more light on the special examples we mentioned above. case...when===

3. Practical application

And with any syntax, to really grasp how it works, we need to put it into practice.

There are many different ways to put it into practice, especially in cases where we need to test the condition that the element satisfies a set of different values.

In the example below, we’ll take an example of checking whether an HTTP response is successful or not based on the status code.

class SuccessRequest
  def self.=== response
    200 <= response.status < 300
  end
end

case http_response
when SuccessRequest
  p 'Nice'
when ...
...

Hopefully, the sharing of syntax will help you in the process of self-study, as well as practical application in daily work.

If you have any questions or concerns, don’t hesitate to leave a comment in the comment section.

And stay tuned for our next posts!

VAREAL Vietnam

AI-first software company — building intelligent solutions with AI at the core.

MST: 0108704322 — Hà Nội

Services

AI Development

Process Automation

Web & Enterprise

AI Consulting

Contact

contact@vareal.vn

(+84) 982 894 859

Cau Giay, Hanoi

© 2026 Vareal Vietnam Co., Ltd. All rights reserved.

Legal Representative: Teramoto Masahiro — Chairman