Enrique Comba Riepenhausen
5/10/2007 1:10:00 PM
On 10 May 2007, at 14:33, James Edward Gray II wrote:
> On May 10, 2007, at 1:56 AM, Enrique Comba Riepenhausen wrote:
>
>> I was thinking about the Factory Method Pattern and following came
>> to my mind.
>
> In addition to Pat's great answer, I think it's important to note
> that classes are objects in Ruby. This often aids in Factory
> pattern scenarios. For example:
>
> >> class Prose
> >> def initialize(title, author)
> >> @title = title
> >> @author = author
> >> end
> >> def inspect
> >> "#{self.class}: #{@title} by #{@author}"
> >> end
> >> end
> => nil
> >> class Novel < Prose; end
> => nil
> >> class ShortStory < Prose; end
> => nil
> >> class Author
> >> def initialize(name)
> >> @name = name
> >> end
> >> def to_s
> >> @name
> >> end
> >> def write(type, title)
> >> type.new(title, self)
> >> end
> >> end
> => nil
> >> james = Author.new("James")
> => #<Author:0x14a11d0 @name="James">
> >> james.write(Novel, "Ruby: The Girl I Loved")
> => Novel: Ruby: The Girl I Loved by James
> >> james.write(ShortStory, "Little Ruby Riding Hood")
> => ShortStory: Little Ruby Riding Hood by James
>
> Just some more food for thought.
>
> James Edward Gray II
Thanks a lot James, I think this clarifies (at least from the newbie
point of view) a lot of the power under the hood in Ruby!
I'm now thinking where to include this in the Wiki.
Any idea?
Cheers, and thanks again!
>
>