Prefer Class Method or Mix-In Instance Method?

Assume that a certain functionality is available both as a class method and as an instance method (via a module).

Class method:

require 'foo'  

Instance method (via module):

require 'bar'    
include Bar    

Assuming that the two methods behave identically, which would you prefer using, and why?

(Note that the question is not about which to implement, but instead about which to use if both available.)

I prefer the Foo example since it doesn’t affect everything. The Bar example is including Bar into the Object class as a private method and Object#bar becomes available everywhere. I think protecting code in contained scopes is both beautiful and a safer way to develop.

Of course if you’re looking for Module vs Class as a topic they can both emulate a singleton. The following all behave the same:

class Moo
  def self.moo()
     # ..


module Mar
  def self.mar()
    # ..


Whenever I’m only ever going to use one instance of anything I like to use a module as I’ve shown above.

I’d use the class version to avoid defining Object#bar. If you’re already in a class then I’d use the module include version as it will remain scoped to that class.