ダックタイピング
青木さんの添削記事で一番きになったのは、やっぱり「クラスでの判別はダメ」ということ。
となると、こんなふうに、ダックタイピング使うしかなさそう。
・・・って、↓のこんなんアリなの???
class Object def to_color return nil end end class String def to_color case self when /\A\[\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\]\z/ then return [$1.to_i, $2.to_i, $3.to_i] when /\A\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\z/ then return [$1.to_i, $2.to_i, $3.to_i] when /\A\#([\da-fA-F]{6})\z/ then return [$1[1,2].hex, $1[3,2].hex, $1[5,2].hex] else color = @@string2color[str.to_sym] or raise MiyakoError, "Illegal parameter" return color end end end class Integer def to_color return [(self % 0x1000000) / 0x10000, (self % 0x10000) / 0x100, self % 0x100] end end class Symbol def to_color return @@string2color[self] end end class Array def to_color return self[0,3] end end