开发者

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

What's the difference between string.match(regex) and regex.match(string) in Ruby? What's the justification for having both those constru开发者_如何学编程cts in the language?


Aside from hanging off of different objects (which sometimes makes it more convenient to call one instead of the other), they are the same. The justification is that they are both useful and one is sometimes more convenient than the other.


I thnk that, intuitively, match, or the related method =~, expresses some kind of equality, as reflected in the fact that =~ includes the equality = and the equivalence ~ relations (not in ruby but in mathematics). But it is not totally an equivalence relation, and among the three axioms of equality (reflexivity, commutativity, transitivity), particularly commutativity seems reasonable to be maintaind in this relation; it is natural for a programmer to expect that string.match(regex) or string =~ regex would mean the same thing as regex.match(string) or regex =~ string. I myself, would have problem remembering if either is defined and not the other. In fact, some people feel it strange that the method ===, which also reminds us of some kind of equality, is not commutative, and have raised questions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜