From: "David A. Black" Date: 2008-09-30T18:32:59+09:00 Subject: Re: extend keyword in class definition Hi -- On Tue, 30 Sep 2008, ruud grosmann wrote: > Hi list, > > I am reading the book 'design patterns in ruby'. On page 201 I find > this code example: > > require 'forwardable' > > class WriterDecorator > extend Forwardable > > def_delegators :@real_writer, :write_line, :rewind, :pos, :close > > def initialize (real_writer) > @real_writer = real_writer > end > end > > > I am a bit surprised by the 'extend Forwardable' line. Can anybody > explain me what this is? Is it the same as 'include Forwardable' or is > it the same as 'class WriterDecorator < Forwardable'? It's like a per-object include operation. module M def speak puts "Hi!" end end obj = Object.new obj.extend(M) obj.speak # "Hi!" The example you've got extends the class object WriterDecorator. It's like this: class C extend M end C.speak # "Hi!" David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL * * Co-taught with Patrick Ewing! See http://www.rubypal.com for details and updates!