JP Boily

Founder of Metrics Watch

Jack of all trade SaaS consultant (but mostly code, analytics & marketing)

March 30, 2014

Hey Rubygems, do not include your stuff implicitly (and don't class_eval Class!)

March 30, 2014 - JP -  

A lot of great gems do this: they automatically include themselves into ActiveRecord::Base, or even worse, into Object or Class directly. I am looking at you, state_machine. In some cases, it is enough for me to not use it (still looking at you, state_machine).

I love people giving their time & code, I really do and appreciate the work. That said, please, let me include your awesome work explicitly where I want/need to.

Having code that speaks by himself and is clear in it’s intention is worth a whole lot. Code where you explicitly include modules is much more clear and easy to maintain too! Using gems that play it nicely and don’t pollute anything else that is not required is wayyy better in my opinion.

I prefer:

class DockerCreator
  include MyAwesomeStateMachine

  states :created, :booting, :stopping, :stopped, :exploded, :deleted, :archived

  transition from: :created,                 to: [:booting, :exploded, :deleted, :archived]
  transition from: :booting,                 to: [:stopping, :exploded, :deleted, :archived]
  transition from: :stopping,                to: [:stopped, :exploded, :deleted, :archived]
end

over:

class DockerCreator
  states :created, :booting, :stopping, :stopped, :exploded, :deleted, :archived

  transition from: :created,                 to: [:booting, :exploded, :deleted, :archived]
  transition from: :booting,                 to: [:stopping, :exploded, :deleted, :archived]
  transition from: :stopping,                to: [:stopped, :exploded, :deleted, :archived]
end

Common, where is that coming from? Seriously? On Class? On Object? In this case, this is Class.

Please, please, don’t do this. Thanks!