I am writing a reporting gem that will have some helpers to extract data from ActiveRecord.
One function it will implement is to_report_yaml wich I need to be called from a single record (ActiveRecord::Base instance) or from an Array of records. The implementation of to_report_yaml is pretty similar in both cases so what I want is to implement it once and use duck typing to make some decisions in my logic.
My question is. What is the best way to extend ActiveRecord::Base and Array so both have a funcion called to_report_yaml pointing to the same implementation?
You should implement your function in a module and include it into both classes. This concept is called Mixins. It is used increasingly everywhere in Ruby libraries and is Ruby’s answer to multi-inheritance (which is a broader concept, but generally harder to understand with it’s edge cases).