2016年8月29日月曜日

開発環境

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 コメント:

コメントを投稿