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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:14:43+00:00 2026-05-25T13:14:43+00:00

In the following code, the issue is that after calling method .find_name on an

  • 0

In the following code, the issue is that after calling method .find_name on an object type of LogsCollection, the returned object becomes a native array and does not remain type LogsCollection. I believe the correct approach might be to create a constructor/initializer that accepts an array and return a brand new object of the correct type. But I am not sure there is not a better way to accomplish this?

Can a Ruby-pro eyeball this code and suggest (at the code level) the best way to make the returned object from .find_name remain type LogsCollection (not array)?

class Log
    attr_accessor :name, :expense_id, :is_excluded, :amount, :paid_to
    def initialize(name, expense_id, is_excluded, amount, paid_to)
        @name = name
        @expense_id = expense_id
        @is_excluded = is_excluded
        @amount = amount
        @paid_to = paid_to
    end
end

class LogsCollection < Array
    def names
        collect do |i|
            i.name
        end
    end

    def find_name(name)
        @name = name
        self.select { |l| l.name == @name }
    end
end

logs = LogsCollection.new
logs.push(Log.new('Smith', 1, false, 323.95, nil))
logs.push(Log.new('Jones', 1, false, 1000, nil))

logs = logs.find_name('Smith')
puts logs.count
unless logs.empty?
    puts logs.first.name # works since this is a standard function in native array
    puts logs.names # TODO: figure out why this fails (we lost custom class methods--LogsCollection def find_name returns _native_ array, not type LogsCollection)
end

Final code post-answer for anyone searching (note the removal of base class < array):

class Log
    attr_accessor :name, :expense_id, :is_excluded, :amount, :paid_to
    def initialize(name, expense_id, is_excluded, amount, paid_to)
        @name = name
        @expense_id = expense_id
        @is_excluded = is_excluded
        @amount = amount
        @paid_to = paid_to
    end
end

class LogsCollection
    attr_reader :logs

    def initialize(logs)
        @logs = logs
    end

    def add(log)
        @logs.push(log)
    end

    def names
        @logs.collect { |l| l.name }
    end

    def find_name(name)     
        LogsCollection.new(@logs.select { |l| l.name == name })
    end
end

logs = LogsCollection.new([])

logs.add(Log.new('Smith', 1, false, 323.95, nil))
logs.add(Log.new('Jones', 1, false, 1000, nil))
puts logs.names

puts '--- post .find_name ---'

puts logs.find_name('Smith').names
  • 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-25T13:14:44+00:00Added an answer on May 25, 2026 at 1:14 pm

    As you can see in the docs Enumerable#select with a block always returns an array. E.g.

    {:a => 1, :b => 2, :c => 3}.select { |k,v | v > 1 }
    => [[:b, 2], [:c, 3]]
    

    What you could do is have some sort of constructor for LogsCollection that wraps up a normal array as a LogsCollection object and call that in find_name.

    As requested here’s an example class (I’m at work and writing this while waiting for something to finish, it’s completely untested):

    class LogsCollection
      attr_reader :logs
    
      def initialize(logs)
        @logs = logs
      end
    
      def names
        @logs.collect { |i| i.name }
      end
    
      def find_name(n)
        name = n
        LogsCollection.new(@logs.select { |l| l.name == n })
      end
    
      # if we don't know a method, forward it to the @logs array
      def method_missing(m, *args, &block)
        @logs.send(m, args, block)
      end
    end
    

    Use like

    lc = LogsCollection.new
    logs = lc.logs.find_name('Smith')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using
I'm trying to track down an issue in our system and the following code
Ok. I'm having an issue with the following bit of code: StreamReader arrComputer =
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
My issue is with a certain style of code that very much resembles recursion,
The following code waits for data over UDP. I have a test function that
Using Internet Explorer (don't appear to have an issue in FireFox) the following code
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

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.