ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

`extend`ing objects at runtime

Open marcandre opened this issue 5 years ago • 2 comments

I've always avoided extending objects at runtime, like in this example:

class Polymorphic
  def initialize(style: :foo)
    extend Something if style == :bar
    # ...
  end
end

It used to be that performance could be severely impacted but it's not clear it's still the case today (see https://github.com/rubocop-hq/rubocop/pull/8881 ) Style-wise, it seems to complicate things and make introspection / debugging / branch-covering more difficult.

Is that an accepted principle to avoid this (and if so, is it still valid)?

marcandre avatar Oct 10 '20 05:10 marcandre

I agree that's something to be avoided. I feel similarly about requiring stuff outside of the top-level scope.

bbatsov avatar Oct 21 '20 17:10 bbatsov

requiring stuff outside of the top-level scope.

I feel this is a very different issue (and I'd love to argue in favor of that if you want to open an issue 😆 )

marcandre avatar Oct 21 '20 17:10 marcandre