pascalmail
pascalmail

Reputation: 43

Why ActiveSupport core extension not added as module?

I was reading the ActiveSupport core_ext source, and saw that it directly open and extend the core ruby class, e.g: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/conversions.rb . Doesn't that make it harder for us to know wether some method is from activesupport or actually provided by ruby itself (e.g via Method#owner)?

Why doesn't it use something like prepending/including a module to give its added functionality? E.g:

module StringConversionExtension
  def to_time
    # some implementation
  end
end

String.prepend(StringExtension)

Is there any historical/performance reason the implementation is as it is now?

Upvotes: 1

Views: 144

Answers (1)

D. SM
D. SM

Reputation: 14520

Why doesn't it use something like prepending/including a module to give its added functionality? E.g:

Possibly to either:

  • Minimize the amount of code in Rails (since your proposed approach is a bit more verbose), or
  • Minimize the number of methods/constants/modules in existence as well as in the core classes, since in your proposed approach the core String class would have the new method and it would now be including the StringExtensions module.

Upvotes: 0

Related Questions