Posted 3 months ago. Visible to the public.

Ruby: Comparing a string or regex with another string

In Rubocop you might notice the cop Style/CaseEquality for e.g. this example:

Copy
def foo(expected, actual) expected === actual end

In case expected is a Regex, it suggests to change it to the following pattern:

Copy
def foo(expected, actual) expected.match?(actual) end

In case expected is a Regex or a String, you need to keep ===. Otherwise the actual expression is always converted to a regular expression.

Copy
# For expected === actual foo('Test(s)', 'Test(s)') #=> true # For expected.match?(actual) foo('Test(s)', 'Test(s)') #=> false

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Emanuel De
Last edit:
3 months ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more