Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:58:15+00:00 2026-05-26T23:58:15+00:00

I am writing a reporting gem that will have some helpers to extract data

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:58:16+00:00Added an answer on May 26, 2026 at 11:58 pm

    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).

    module MyModule
      def to_report_yaml
        self.to_yaml # or whatever
      end
    end
    
    class ActiveRecord::Base
      include MyModule
    end
    
    class Array
      include MyModule
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that will call a function from an external library,
I'm writing reports on SQL Server Reporting Server that have a number of hours
I'm faced with needing access for reporting to some data that lives in Oracle
I am writing some library functions that needs to do some error reporting. I
Im writing a report with Reporting services that is almost always only going to
I am writing a custom error handling / reporting function for PHP file upload
I am writing some code that needs to be backwards compatible with EXISTING remoting
The linker is reporting multiply defined errors for an inline function. I have the
I'm writing desktop app in c# and I need some free reporting system. Any
I'm writing an XSL transform in Visual Studio. It is reporting that the semicolon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.