中級 › Comparable を include しても <=> が無いと比較演算子は使えない › パターン3
Comparable を include しても <=> が無いと比較演算子は使えない
Comparableモジュールをincludeするだけでは <、>、between? などのメソッドは動きません。
1 class Score 2 include Comparable 3 attr_reader :points 4 def initialize(points) 5 @points = points 6 end 7 8 __________________________________________________ ^ 9 end 10 11 puts Score.new(10) < Score.new(20)
main.rb:11:in `<': comparison of Score with Score failed (ArgumentError)
from main.rb:11:in `<main>'
空欄を埋めて実行を通す
「includeした時点で比較演算子が使えるようになるはず」という思い込みは誤りです。<=>の実装は自分の責任です。
次の問題