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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:52:25+00:00 2026-05-11T07:52:25+00:00

I have a class that I want to compare to both strings and symbols

  • 0

I have a class that I want to compare to both strings and symbols in a case statement, so I thought that I just override the ===() method for my class and all would be gold. However my ===() method never gets called during the case statement. Any ideas?

Here is some example code, and what happens in a irb session:

class A    def initialize(x)       @x=x #note this isn't even required for this example    end    def ===(other)       puts 'in ==='       return true    end end 

irb(main):010:0> a=A.new(‘hi’)
=> #
irb(main):011:0> case a
irb(main):012:1> when ‘hi’ then 1
irb(main):013:1> else 2
irb(main):014:1> end
=> 2

(it never prints the message and should always return true anyway) Note that ideally I’d like to do a

def ===(other)           #puts 'in ==='           return @x.===(other) end 

Thanks in advance.

  • 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. 2026-05-11T07:52:26+00:00Added an answer on May 11, 2026 at 7:52 am

    The expression after the ‘case’ keyword is the right hand side of the === expression, and the expression after the ‘when’ keyword is on the left hand side of the expression. So, the method that is being called is String.===, not A.===.

    A quick approach to reversing the comparison:

    class Revcomp     def initialize(obj)         @obj = obj     end      def ===(other)         other === @obj     end      def self.rev(obj)         Revcomp.new(obj)     end end  class Test     def ===(other)         puts 'here'     end end  t = Test.new  case t when Revcomp.rev('abc')     puts 'there' else     puts 'somewhere' end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that inherits QMainWindow and I just want it to have
I have one class and I want compare two objects of that class. Is
I have a class that I want to contain multiple objects of something I
I have a class that I want to include in a GWT module. Unfortunately,
I have a templated class that I want to avoid copying (because of the
I have a class PlaylistTrack that I want to extend class Song. When I
I have a helper class that I want to display a UIAlertView then will
Suppose I have a Python class that I want to add an extra property
I have a signed class library that I want to create assemblies for via
I have a Generic Base Class that I want to allow one of two

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.