開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Ruby 2.3 (プログラミング言語)
Head First Ruby (Jay McGavren (著)、O'Reilly Media)のChapter 10.(Comparable and Enumerable: Ready-MadeMixes)、How the Comparable methods work、POOL PUZZLE(No. 5470)を取り組んでみる。
POOL PUZZLE(No. 5470)
コード(Emacs)
#!/usr/bin/env ruby2.3
# -*- coding: utf-8 -*-
class Apple
include Comparable
attr_accessor :weight
def initialize(weight)
self.weight = weight
end
def <=>(other)
self.weight <=> other.weight
end
end
small_apple = Apple.new(0.17)
medium_apple = Apple.new(0.22)
big_apple = Apple.new(0.25)
puts "small_apple > medium_apple:"
puts small_apple > medium_apple
puts "medium_apple < big_apple:"
puts medium_apple < big_apple
入出力結果(Terminal)
$ ./sample1.rb small_apple > medium_apple: false medium_apple < big_apple: true $
0 コメント:
コメントを投稿