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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:09:19+00:00 2026-05-16T21:09:19+00:00

I have a skeleton class: class Foo def bar # returns some sort of

  • 0

I have a skeleton class:

class Foo
   def bar
    # returns some sort of array
   end
end

but how can one add the ‘writer’ method to ‘bar’ so to enable the Array#push behavior?

Foo.new.bar<<['Smile']
_.bar #=> ['Smile']

EDITED:
I should expand my question further.
There are two classes. Foo, and Bar, much like the ActiveRecord has_many relation where Foo has_many Bars

But I am actually storing the ids of Bar inside a method of Foo. I name that method bar_ids

so @foo = Foo.new(:bar_ids => [1,2,3])

As you can imagine, if I ever want to look up what Bars belong to @foo, I have to actually do something like Bar.where(:id => @foo.bar_ids)

So I decided to make another method just named bar to do just that
class Foo
#…
def bar
Bar.where(:id => bar_ids)
end
end

That worked out. now I can do @foo.bar #=> all the bars belonging to @foo

Now I also want to have that kind of push method like ActiveRecord associations, just to cut out the “id” typing when associating another bar object to a foo object

Currently, this works:
@foo.bar_ids << Bar.new.id
@foo.save

But I want:
@foo.bar << Bar.new #where the new bar’s id will get pushed in the bar_ids method of @foo
@foo.save

Thanks for all of your help, I really appreciate your thoughts on this!

  • 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-16T21:09:19+00:00Added an answer on May 16, 2026 at 9:09 pm
    class Foo
       attr_reader :bar
       def initialize
         @bar = Array.new
         def @bar.<< arg
           self.push arg.id
         end
       end
    
    end
    
    class Bar
      attr_accessor :id
      def initialize id
        self.id = id
      end
    end
    
    
    f = Foo.new
    bars = (1..5).map{|i| Bar.new i}
    
    f.bar << bars[2]
    f.bar << bars[4]
    
    p f.bar  #=> [3, 5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following skeleton of a class. As you can see in the
I have a simple class that holds skeleton for a much larger, bulkier class.
I have write a small python module with one class and two functions. The
I am new to jQuery but have a question I hope some of you
I have two classes, GameCharacter and Skeleton. One of the properties on GameCharacter is
I am using some threads in java android, I have a class which implements
I have an abstract generic class public abstract class Foo<TType> with an abstract method
I am developing on a Mac with Eclipse and have the skeleton for an
I have a linear layout ( layout_container in skeleton.xml) which is inflated with a
i'm building a wordpress webpage based on the Skeleton Wordpress theme. I have 2

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.