class ChineseNumber Numbers = ["一","二","三","四","五","六","七","八","九","十"] attr :chinese_number def initialize(value) if Numbers.include?(value) @chinese_number = value else raise "数字不正确" end end def to_num #转化为整数 Numbers.index(@chinese_number)+1 if @chinese_number end def to_s @chinese_number end def succ #返回序列中的下一个对象 (Range类必须要的方法) raise(IndexError,"超出了数字范围") if self.to_num>=10 ChineseNumber.new(Numbers[self.to_num]) end def <=>(other) #比较两个对象的大小 (Range类必须要的方法) self.to_num <=> other.to_num end end def ChineseNumber(val) #用来创建的工具方法 ChineseNumber.new(val) end one = ChineseNumber("一") four = ChineseNumber("四") puts "#{one} => #{one.to_num}" puts "#{one} 的下一个是 #{one.succ}" range = one..four puts range.include?(ChineseNumber("三")) range.each{|i| puts i }