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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:33:10+00:00 2026-06-01T18:33:10+00:00

According to the Set doc , elements in a set are compared using eql?

  • 0

According to the Set doc, elements in a set are compared using eql?.

I have a class like:

class Foo
  attr_accessor :bar, :baz

  def initialize(bar = 1, baz = 2)
    @bar = bar
    @baz = baz
  end

  def eql?(foo)
    bar == foo.bar && baz == foo.baz
  end
end

In console:

f1 = Foo.new
f2 = Foo.new
f1.eql? f2 #=> true

But…

 s = Set.new
 s << f1
 s << f2
 s.size #=> 2

Because f1 equals f2, s should not include both of them.

How to make the set reject elements with a custom rule?

  • 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-06-01T18:33:12+00:00Added an answer on June 1, 2026 at 6:33 pm

    The docs that you link to say explicitly (emphasis mine):

    The equality of each couple of elements is determined according to Object#eql?
    and Object#hash, since Set uses Hash as storage.

    If you add a hash method to your class that returns the same value for eql? objects, it works:

    # With your current class
    
    f1, f2 = Foo.new, Foo.new
    p f1.eql?(f2)
    #=> true
    p f1.hash==f2.hash
    #=> false
    p Set[f1,f2].length
    #=> 2
    
    # Fix the problem
    class Foo
      def hash
        [bar,hash].hash
      end
    end
    
    f1, f2 = Foo.new, Foo.new
    p f1.eql?(f2)
    #=> true
    p f1.hash==f2.hash
    #=> true
    p Set[f1,f2].length
    #=> 1
    

    To be honest I’ve never had a great sense for how to write a good custom hash method when multiple values are involved.

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

Sidebar

Related Questions

I have this container div whose width I would like to set according to
According to doc, calendar set() is: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29 set(int year, int month, int date) Sets
I am using oracle 10. I need to sort my result set according to
I have a line set cpoptions+=$ in my .vimrc file. However according to verbose
According to Telerik's documentation, to set a RadDatePicker's mindate or maxdate properties client-side, you
I'm trying to set default scope according to some criteria determined by ana ActionController
I got a set of 20 queries that each one (according to log takes
According to the DCPU specification , the only time a SET instruction fails is
In Java we use System.setProperty() method to set some system properties. According to this
In my .vimrc, I set different makeprg according to the source file type as

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.