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

  • Home
  • SEARCH
  • 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 9019343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:45:13+00:00 2026-06-16T04:45:13+00:00

I have a class in which the data is stored as a set and

  • 0

I have a class in which the data is stored as a set and I want to be able to compare objects of that class such that the letter case of the elements is of no matter. For example if the set contains elements that are strings there should be no difference of "a" and "A".

To do this I have tried to define the eql? method of the set members to be insensitive to case but this has no effect on the method - (alias difference) in Set. So, how should I go about to make - insensitive to case?

The following code illustrates the problem:

require 'set'

class SomeSet
  include Enumerable

  def initialize; @elements = Set.new; end

  def add(o)
    @elements.add(o)
    self
  end

  def each(&block)              # To enable +Enumerable+
    @elements.each(&block)
  end

  def difference(compared_list)
    @elements - compared_list
  end
end

class Element
  attr_reader :element

  def initialize(element); @element = element; end

  # This seems to have no effect on +difference+
  def eql?(other_element)
    element.casecmp(other_element.element) == 0
  end
end

set1 = SomeSet.new
set2 = SomeSet.new
set1.add("a")
set2.add("A")

# The following turns out false but I want it to turn out true as case
# should not matter.
puts set1.difference(set2).empty?
  • 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-16T04:45:15+00:00Added an answer on June 16, 2026 at 4:45 am

    Ok, firstly, you’re just storing strings from SomeSet#add, you need to store an instance of Element, like so:

    def add(o)
      @elements.add(Element.new(o))
      self
    end
    

    And you need to implement a hash method in your Element class.

    You can convert Element#@element to lowercase, and pass on its hash.

    def hash
      element.downcase.hash
    end
    

    Full code and demo: http://codepad.org/PffThml2

    Edit: For my O(n) insertion comment, above:

    Insertions are O(1). From what I can see, eql? is only used with the hash of 2 elements is same. As we’re doing hash on the downcased version of the element, it will be fairly well distributed, and eql? shouldn’t be called much (if it is called at all).

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

Sidebar

Related Questions

I want to have a class that defines my parameter data for a stored
I have a Core Data class Meeting with a property date which I want
I have a Java class which handles both single valued and multi-valued data. Therefore,
I have a manager class, which update json data from web, get values... The
I have a data structure which is as given below: class File { public
I have a project for my Data Structures class, which is a file compressor
Suppose I have a class MyClass to which I want to add certain 'observer'
I have several stored procedures which return a strongly typed result set. I've learned
The use case is that I have a remote (read slow) database from which
I have class which have one public method Start , one private method and

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.