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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:21:56+00:00 2026-05-17T02:21:56+00:00

According to the Ruby Set class’s documentation, == Returns true if two sets are

  • 0

According to the Ruby Set class’s documentation, “== Returns true if two sets are equal. The equality of each couple of elements is defined according to Object#eql?.

The essence of this can be demonstrated using Date objects, where sets containing different Date objects but with the same date compare to equal:

require 'set'
d1 = Date.today              # => Thu, 30 Sep 2010
puts d1.object_id            # => 2211539680
d2 = Date.today + 1          # => Fri, 01 Oct 2010
puts d2.object_id            # => 2211522320
set1 = Set.new([d1, d2])

d11 = Date.today             # => Thu, 30 Sep 2010
puts d11.object_id           # => 2211489080
d12 = Date.today + 1         # => Fri, 01 Oct 2010
puts d12.object_id           # => 2211469380
set2 = Set.new([d12, d11])

set1 == set2                 # => true

But using my own objects, where I’ve coded the eql? method to only compare certain attributes, I can’t get it to work.

class IpDet

    attr_reader :ip, :gateway

    def initialize(ip, gateway, netmask, reverse)
        @ip = ip
        @gateway = gateway
        @netmask = netmask
        @reverse = reverse
    end

    def eql?(other)
        if @ip = other.ip && @gateway == other.gateway
            return true
        else
            return false
        end
    end
end



ipdet1 = IpDet.new(123456, 123457, 123458, 'example.com')
ipdet2 = IpDet.new(234567, 2345699, 123458, 'nil')

ipdet11 = IpDet.new(123456, 123457, 777777, 'other_domain.com')
ipdet12 = IpDet.new(234567, 2345699, 777777, 'example.com')

puts "ipdet1 is equal to ipdet11: #{ipdet1.eql?(ipdet11)}"
puts "ipdet2 is equal to ipdet12: #{ipdet2.eql?(ipdet12)}"


set1 = Set.new([ipdet1, ipdet2])
set2 = Set.new([ipdet11, ipdet12])

puts "set 1 is equal to set2: #{set1 == set2}"

The output I get from the above is:

ipdet1 is equal to ipdet11: true
ipdet2 is equal to ipdet12: true
set 1 is equal to set2: false

Any ideas anyone?

  • 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-17T02:21:57+00:00Added an answer on May 17, 2026 at 2:21 am

    When you override eql?, you also always need to override hash such that if o1.eql?(o2) is true, o1.hash == o2.hash is also true.

    For example your hash method could look like this:

    def hash
      [@ip, @gateway].hash
    end
    

    Also you have a typo in your eql? method: @ip = other.ip should be @ip == other.ip.

    Also a minor stylenote: if condition then true else false end is equivalent to just condition, so your eql? method can just be defined as

    def eql?(other)
      @ip == other.ip && @gateway == other.gateway
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are Strings mutable in Ruby? According to the documentation doing str = hello str
According to the Ruby docs for Time#zone : As of Ruby 1.8, returns UTC
Bundler 1.2.0.pre includes a new ruby DSL option. According to Heroku's documentation , they
According to Hibernate documentation : After observing that arrays cannot be lazy , you
According to the Ruby on Rails 2.3 Release Notes... if your plugin has an
Currently, the ItemCollection#query method in the Ruby AWS SDK returns the whole dataset. I
According to The Ruby Programming Language p.164. If a begin statement doesn't propagate an
According to Charles Nutter , Duby is a static-typed language with Ruby's syntax and
I'd like to: Represent a DAG according to The Ruby Way. Generate an image
according to your experiences what's the better choice to access memcached from ruby? Thank

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.