module TimeStampingWriter
def write_line(line)
super("#{Time.new}: #{line} b")
end
end
module NumberingWriter
attr_reader :line_number
def write_line(line)
@line_number = 1 unless @line_number
super("#{@line_number}: #{line} a")
@line_number += 1
end
end
class SimpleWriter
def write_line(line)
puts "#{line}"
end
end
w1 = SimpleWriter.new
w1.extend(TimeStampingWriter)
w1.write_line('hello')
w2 = SimpleWriter.new
w2.extend(NumberingWriter)
w2.write_line('hello')
w = SimpleWriter.new
w.extend(TimeStampingWriter)
w.extend(NumberingWriter)
w.write_line('hello')
输出: 2014-11-03 17:00:19 +0800: hello b 1: hello a 2014-11-03 17:00:19 +0800: 1: hello a b
extend 两个模块后的输出很疑惑,求解释。