In Ruby, how to implement global behaviour?

Posted by Gordon McAllister on Stack Overflow See other posts from Stack Overflow or by Gordon McAllister
Published on 2010-05-07T09:56:39Z Indexed on 2010/05/07 10:08 UTC
Read the original article Hit count: 418

Hi all,

I want to implement the concept of a Workspace. This is a global concept - all other code will interact with one instance of this Workspace. The Workspace will be responsible for maintaining the current system state (i.e. interacting with the system model, persisting the system state etc)

So what's the best design strategy for my Workspace, bearing in mind this will have to be testable (using RSpec now, but happy to look at alternatives).

Having read thru some open source projects out there and I've seen 3 strategies. None of which I can identify as "the best practice".

They are:

  1. Include the singleton class. But how testable is this? Will the global state of Workspace change between tests?
  2. Implemented all behaviour as class methods. Again how do you test this?
  3. Implemented all behaviour as module methods. Not sure about this one at all!

Which is best? Or is there another way?

Thanks, Gordon

© Stack Overflow or respective owner

Related posts about ruby

Related posts about best-practices