Posted about 1 month 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

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Emanuel De
Last edit:
about 1 month 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