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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:48:16+00:00 2026-05-28T06:48:16+00:00

Given I’ve got an array arr of instances of a custom class My_class .

  • 0

Given I’ve got an array arr of instances of a custom class My_class. This class has got a couple of different instance variables. Let’s call them :name (a String), :is_processed (a Boolean) and :date (a DateTime). All of them are readable.

What is the best way of checking, whether another given instance of My_class (call it newbe) is already in the array based on the content of the aforementioned instance variables?

Using arr.include?(newbe) will not work as this compares the object IDs, doesn’t it?
Can I override the way Array#include? compares two objects?


Speaking in code

class My_class
  attr_reader :name, :is_processed, :date

  def initialize(_name, _proc, _d)
    @name = _name
    @is_processed = _proc
    @date = _d
  end
end

somewhere else

first = My_class.new("First", false, DateTime.new(2001,2,3,4,5,6))
second = My_class.new("Second", true, DateTime.new(2001,2,3,4,5,6))
third = My_class.new("Third", false, DateTime.new(2001,2,3,3,2,1))
newbe = My_class.new("Second", true, DateTime.new(2001,2,3,4,5,6))

arr = [first, second, third]
arr.include?(newbe)  # => false but should be true
  • 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-28T06:48:17+00:00Added an answer on May 28, 2026 at 6:48 am

    You want to compare instances of you class, so include comparable, define a <=> method and you have added these methods to your class instances:

    <, <=, ==, >, >=, between? (#include? uses #== ).

    require 'date'
    class My_class
      include Comparable
      attr_reader :name, :is_processed, :date
    
      def initialize(_name, _proc, _d)
        @name = _name
        @is_processed = _proc
        @date = _d
      end
    
      def <=>(other)
        [self.name, self.is_processed, self.date]<=>[other.name, other.is_processed, other.date]
      end
    end
    first = My_class.new("First", false, DateTime.new(2001,2,3,4,5,6))
    second = My_class.new("Second", true, DateTime.new(2001,2,3,4,5,6))
    third = My_class.new("Third", false, DateTime.new(2001,2,3,3,2,1))
    newbe = My_class.new("Second", true, DateTime.new(2001,2,3,4,5,6))
    
    arr = [first, second, third]
    p arr.include?(newbe)  # => true
    #you could do arr.sort, but they are sorted allready by accident...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this class: class First def to_s ; Hello World ; end end and
Given this class: class Foo { readonly ILog log; public Foo(ILog log) { this.log
Given a (source) patch file, what's the easiest way to apply this patch on
Given this HTML: <ul id=topnav> <li id=topnav_galleries><a href=#>Galleries</a></li> <li id=topnav_information><a href=#>Information</a></li> </ul> And this
Given a abstract factory implementation: public class FooFactory : IFooFactory { public IFoo Create(object
Given this: $ids = ''; I just realized that this: $single = $ids ==
Given a class: class foo { public string a = ; public int b
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Given that I have the array Let Sum be 16 dintptr = { 0
Given this Python program: num = input(Enter a number: ) result = 1024 for

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.