RubyFlow The Ruby and Rails community linklog

Design Patterns written in Ruby language

My collection of Design Patterns written in Ruby. Previously they were implemented in Beanshell language. Also, a book dedicated to this topic.

Comments

You’re basically programming Java in Ruby.

Here’s a (simplified) version of the observer pattern using blocks/lamdas. If you want to un-register a listener, you need to keep a reference around to the lambda you registered as a listener.

http://gist.github.com/83038

If you’re serious about this, I suggest you make your examples a bit more rubyish :)

I think it is interesting though to see how classic design patterns are implemented in ruby, in a straightforward manner. And then think how one can use blocks/lambdas to make them more adaptable.

I added some meta-programming hackery (which I’m not great at, there might be an even better way of doing this) to add observers in a more rails-like fashion.

http://gist.github.com/83176

@fredrik - You could argue these patterns are less Rails-ish perhaps but Rails isn’t everyones idea of what Ruby should look like. After all Ruby’s standard observer module doesn’t use any meta programming hackery.

I’d argue simple isn’t any less Ruby-ish than exotic or we’d all be writing in the style of _why’s Camping framework. Choose a level of magic so peoples heads don’t explode; programming Perl taught me that :)

I agree with Frederick. It’s nice that you are writing these, but I can’t help but feel the weight of Java imposed on top of Ruby here. It would be a good exersise to find the Ruby way of implementing these.

agree with most people here. sure you can code Ruby so it looks like Java/C# code, most programmers who are new to Ruby do. the point is if you’re going to code java-like in ruby, what’s the point of using ruby? maybe put this on the refactoring site and see how rubyists would code each pattern.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in