String.match (regex) vs regex.match (string)

What is the difference between string.match (regex) and regex.match (string) in Ruby? What is the rationale for the presence of both of these constructs in the language?

+7
source share
2 answers

I thnk, which, intuitively, match or the associated method =~ , expresses some kind of equality, which is reflected in the fact that =~ includes equality = and equivalence relations ~ (not in ruby, but in mathematics). But this is not quite an equivalence relation, and among the three axioms of equality (reflexivity, commutativity, transitivity), it seems reasonable to maintain commutativity in this regard; it is natural for the programmer to expect that string.match(regex) or string =~ regex will mean the same as regex.match(string) or regex =~ string . I myself, I will have problems with remembering, if it is defined, and not another. In fact, it seems strange to some people that the === method, which also reminds us of some kind of equality, is not commutative and raised question s.

+1
source

In addition to hanging different objects (which sometimes makes it more convenient to call one, and not the other), they are the same. The rationale is that they are useful, and one is sometimes more convenient than the other.

+4
source

All Articles