From: Stefano Crocco Date: 2007-08-02T23:42:41+09:00 Subject: Re: Beginner's tutorial and poor attempt at classes Alle gioved� 2 agosto 2007, Gabriel Dragffy ha scritto: > Thank you very much, these work a treat! I've got to now ask the � > inevitable question - why do they work? What difference does it make � > which side the things being compared are? I'm sure I'll come across � > the answer myself as I continue to get better with Ruby, but I'd be � > glad of an explanation sooner :) When ruby sees an operator, for instance a + b, it calls the corresponding method of the first operand, the one on the left, passing the second operand as argument. So, a + b becomes: a.+(b) So the line value === (1..6) becomes value.===((1..6)) value is an integer, so its === method returns true if the other operand is a number with the same value (at least, I think it does, since I haven't been able to find any documentation on it). Instead, the line (1..6) === value becomes (1..6).===(value) and the === method of class range returns true if its argument is an element of the range itself. Another situation in which the order of the operands matters, is, for example, when multiplying a string and a number: 'a' * 2 # this means 'a'.*(2) returns the string 'aa', since the method * of class string returns a string containing a number of copies of the receiver. On the other hand, 2 * 'a' # this means 2.*('a') raises an exception, since the method * for a number only performs number multiplication. I hope this helps Stefano