Friday, July 28, 2006

Ruby decorator pattern

Is any kind of ruby decorator pattern out-of-the-box there?
so far i didn't find any pure decorator pattern in ruby, instead of this all i have found is the ruby-way of decorating.

(Updated June 2006)
So.. times goes by ... and I found this implementation by Jason Arhart using module.

class Foo
def initialize(s)
@s = s
end
def to_s
@s
end
end

module SingleQuoted
def to_s
"'" + super + "'"
end
end

module DoubleQuoted
def to_s
'"' + super + '"'
end
end

foo = Foo.new('foo')
puts "#{foo.id} - #{foo}"
foo.extend(SingleQuoted)
puts "#{foo.id} - #{foo}"
foo.extend(DoubleQuoted)
puts "#{foo.id} - #{foo}"

No comments: