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 776499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:27:32+00:00 2026-05-14T19:27:32+00:00

I would like to write a module that provides active record like functionality on

  • 0

I would like to write a module that provides active record like functionality on an array instance variable.

Examples of its use would be

x = Container.new
x.include(ContainerModule)
x.elements << Element.new
x.elements.find id

module ContainerModule
   def initialize(*args)
     @elements = []
     class << @elements
        def <<(element)
          #do something with the Container...
          super(element)         
        end

        def find(id)
          #find an element using the Container's id
          self
          #=> #<Array..>  but I need #<Container..>
        end
     end
     super(*args)
  end
end

The problem is that I need the Container object within these methods. Any reference to self will return the Array, not the Container object.

Is there any way to do this?

Thanks!

  • 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-14T19:27:33+00:00Added an answer on May 14, 2026 at 7:27 pm

    Would something like this work for you?

    class Container
      attr_accessor :elements
    
      def initialize
        @elements = ContainerElements.new
      end
    end
    
    class ContainerElements < Array
    
      def find_by_id(id)
        self.find {|g| g.id == id }
      end
    
    end
    

    So i create a container-class, and a ContainerElements that inherits from Array, with an added (specific) find_by_id method.
    If you really want to call it find you need to alias it.

    Example code would be:

    class ElemWithId
      attr_accessor :id
      def initialize(value)
        @id = value
      end
    end
    
    cc = Container.new
    cc.elements << ElemWithId.new(1)
    cc.elements << ElemWithId.new(5)
    
    puts "elements = #{cc.elements} "
    puts "Finding: #{cc.elements.find_by_id(5)} "
    

    Hope this helps …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After investigating this, I can now see that the problem… May 16, 2026 at 5:44 pm
  • Editorial Team
    Editorial Team added an answer As has been stated, you are trying to include the… May 16, 2026 at 5:44 pm
  • Editorial Team
    Editorial Team added an answer I used the approach with starting another AppDomain from Main(),… May 16, 2026 at 5:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I would need to write an Authentication Module for IIS7 that behaves exactly like
I'm writing a C# .NET module and I would like to use the provider
I would like to write an android app that basically layers an overlay on
I would like to write a code internal to my method that print which
I have a series of 'tasks' that I would like to run in separate
I'm an iPhone/iPod-touch newbie, and would like to write an iPhone application utilizing the
I use the math module a lot lately. I don't want to write math.sqrt(x)
I would like to write an app on Android to upload my GPS location
My objective is create an apache module that will provide RESTful services (i.e., we
I'm looking for a google search module that uses the 'Google Custom Search API'

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.