Wait a minute, shouldn't Integer#+ call `to_int` on the other value if it's not an Integer?
```
class FourtyTwo
def to_int
42
end
end
p(2 + FourtyTwo.new)
```
```
test.rb:9:in `+': FourtyTwo can't be coerced into Integer (TypeError)
```
@postmodern I believe it calls `coerce` instead, see https://stackoverflow.com/questions/2799571/in-ruby-how-does-coerce-actually-work
@postmodern TIL #coerce and it's implemented on most of the default #ruby classes [0]:
class One
def coerce(other)
[other, 1]
end
def +(other)
other + 1
end
end
p (2 + One.new)
# => 3
p (One.new + 4)
# => 5
Thanks, @mudge
[0] ie. Integer#coerce https://docs.ruby-lang.org/en/3.2/Integer.html#method-i-2B