3 In Ruby, operators such as <code>+</code>, are defined as methods on the class.
4 Literals[rdoc-ref:syntax/literals.rdoc] define their methods within the lower
5 level, C language. String class, for example.
7 Ruby objects can define or overload their own implementation for most operators.
13 self.concat(str).concat("another string")
17 foobar = Foo.new("test ")
22 test baz another string
24 What operators are available is dependent on the implementing class.
28 How a class behaves to a given operator is specific to that class, since
29 operators are method implementations.
31 When using an operator, it's the expression on the left-hand side of the
32 operation that specifies the behavior.
35 3 * 'a' # TypeError: String can't be coerced into Integer
39 Logical operators are not methods, and therefore cannot be
40 redefined/overloaded. They are tokenized at a lower level.
42 Short-circuit logical operators (<code>&&</code>, <code>||</code>,
43 <code>and</code>, and <code>or</code>) do not always result in a boolean value.
44 Similar to blocks, it's the last evaluated expression that defines the result
47 === <code>&&</code>, <code>and</code>
49 Both <code>&&</code>/<code>and</code> operators provide short-circuiting by executing each
50 side of the operator, left to right, and stopping at the first occurrence of a
51 falsey expression. The expression that defines the result is the last one
52 executed, whether it be the final expression, or the first occurrence of a falsey
57 true && 9 && "string" #=> "string"
58 (1 + 2) && nil && "string" #=> nil
59 (a = 1) && (b = false) && (c = "string") #=> false
65 In this last example, <code>c</code> was initialized, but not defined.
67 === <code>||</code>, <code>or</code>
69 The means by which <code>||</code>/<code>or</code> short-circuits, is to return the result of
70 the first expression that is truthy.
74 (1 + 2) || true || "string" #=> 3
75 false || nil || "string" #=> "string"