I have this stupid question about matching operator in Ruby. Why did Ruby make the matching operator =~ instead of ~=? Is there a historical, psychological or other considerations of making the former instead of the later?
I couldn't found any info on this, and I need to make sense about this because I keep typing the wrong thing due to other operator such as +=, -=, !=, >= and <= place the = on the right of the other operator, while =~ is the opposite.
Please enlight me.
My opinion is that it is more consistant. They are several 'equal' and 'match' operators. For instance:
=~ for 'matches'
!~ for 'does not match'
== for 'equals'
!= for 'does not equal'
Also from a far higher level prospective if you think about the way you would read these symbols in words it makes prefect sense:
+= -= are basically saying a = a + b or a = a - b obviously the
addition(+) or subtraction(-) must happen prior to the
assignment(=). >= != these make sense too because
you are asking is a greater than(>) or equal to(=) bor does
a not(!) equal(=) b so why not write it that way. ~= would be like saying does a pattern(~)
match(=) b when what you want is =~ which would read does a
match(=) pattern(~) b.If you read them as words it become fairly succinct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With