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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:15:57+00:00 2026-05-23T03:15:57+00:00

I have a class where I want ‘each’ to yield another custom object, so

  • 0

I have a class where I want ‘each’ to yield another custom object, so I wrote this:

class Fq_price_set < Array
 ...
  def [](i)
    # instead of returning an array, it returns an Fq_price_rec based on the array at i
    Fq_price_rec.new(super(i))
  end  

  def each
    c = 0
    until c == size
      yield self.[](c)
      c += 1
    end
  end
...
end

This works: when I do

my_price_set.each {|rec| puts rec.class}

it shows Fq_price_rec. Similarly,

my_price_set.each {|rec| puts rec.mymethod}

outputs the proper value for that method call.

But when I use select, e.g.,

my_price_set.select {|rec| rec.mymethod == 1}

I get an error msg, “undefined method” ‘mymethod’ for Array:… So rec (in ‘select’) is not an Fq_price_rec, it’s an array (of which Fq_price_rec is a subclass). I (obviously mistakenly) thought that overriding ‘each’ would mean that the iterating methods like ‘select’ would use it, i.e., the subclass’s version of ‘each’. Is the simple answer that I must also override ‘select’, or is there a more elegant solution.

Yes, I’m pretty new at Ruby.

TIA

  • 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-23T03:15:58+00:00Added an answer on May 23, 2026 at 3:15 am

    Why not get rid of inheriting from Array, and just have include Enumerable?

    class Fq_price_set
      include Enumerable
    
      def initialize(actual_array)
        @actual_array = actual_array
      end
    
      # Make [] and each refer to values from @actual_array
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class which I want to pass around Windows as LPARAM parameter.
I have class in server side and I want use method of this class
I have a class and I want to create an array of a number
I want to have class that can be instantiated with list, array, seq, set,
I have a class called Product in my Business object and in another class
I have a class that I want to use to store properties for another
I my project I have a class that in this class I want to
I my project I have a class that in this class I want to
I have this class and I want to get the values in second method
I have class and I want to initalize column that stores pictuer with type

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.