https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112023-02-12T09:57:59ZRuby Issue Tracking SystemRuby master - Misc #16507: =~ vs include? or match?https://bugs.ruby-lang.org/issues/16507?journal_id=1018222023-02-12T09:57:59ZrubyFeedback (robert heiler)
<ul></ul><p>I can't speak for the ruby core team, but here is my rule-of-thumb in my<br>
own ruby code "bases" out there.</p>
<p>I usually try to stick with the simplest choice possible, which in many<br>
ways is .include?() for me. Normally I do not use the () there, but I<br>
just wanted to make it more explicit here.</p>
<p>Sometimes this is not possible, though, such as when the input is not so<br>
simple (e. g. where a regex is more appropriate such as /\d{0,6}.\d{3,6}/<br>
like when the input can vary). Regexes are great in ruby; I think matz<br>
liked them from the perl days. :)</p>
<p>I almost never use .match() or .match?() - and I think these are also<br>
rare in other people's code bases out there.</p>
<p>I tend to use methods ending via "?" a lot. It is a bit like DSL-design<br>
for me:</p>
<pre><code>if object.can_do_this? or object.can_do_that?
end
</code></pre>
<p>I think it reads very nice. That is actually my recommendation too - if<br>
you feel the readability is improved then use the methods.</p>
<p>The method .ci?() seems a bit short-ish. I would recommend against such<br>
short method names; they rarely seem to improve a lot. I tend to use them<br>
mostly when I need to do things where being succinct is important.</p>
<p>.mswin?() makes more sense. Even then I would perhaps consider using<br>
two words, if that makes sense for the code you are working with.</p>
<p>In my opinion, when it really helps you maintain the code, and when it<br>
leads to some clarity, using helper-methods is a good idea.</p>
<p>Perhaps if you want some more feedback from ruby core devs, it may help<br>
if you could give a few more specific examples so they can look at<br>
the instances where you thought that removing =~ would be a good idea.<br>
Sometimes different ruby developers have a different focus and<br>
priority for using certain constructs in their code. For me my own<br>
code is very simple and readable, but I am sure for others it can<br>
be very messy and unreadable (I try to stick to simplicity at all<br>
times and avoid too complicated constructs; for instance I never use<br>
"->" and other things, but I use blocks a LOT, so my code will have<br>
tons of "if block_given?; yield = yielded" just to keep on working<br>
with the variable. Blocks are great).</p>