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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:24:22+00:00 2026-05-25T00:24:22+00:00

I am trying to understand the difference between these four methods. I know by

  • 0

I am trying to understand the difference between these four methods. I know by default that == calls the method equal? which returns true when both operands refer to exactly the same object.

=== by default also calls == which calls equal?… okay, so if all these three methods are not overridden, then I guess
===, == and equal? do exactly the same thing?

Now comes eql?. What does this do (by default)? Does it make a call to the operand’s hash/id?

Why does Ruby have so many equality signs? Are they supposed to differ in semantics?

  • 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-25T00:24:22+00:00Added an answer on May 25, 2026 at 12:24 am

    I’m going to heavily quote the Object documentation here, because I think it has some great explanations. I encourage you to read it, and also the documentation for these methods as they’re overridden in other classes, like String.

    Side note: if you want to try these out for yourself on different objects, use something like this:

    class Object
      def all_equals(o)
        ops = [:==, :===, :eql?, :equal?]
        Hash[ops.map(&:to_s).zip(ops.map {|s| send(s, o) })]
      end
    end
    
    "a".all_equals "a" # => {"=="=>true, "==="=>true, "eql?"=>true, "equal?"=>false}
    

    == — generic "equality"

    At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendant classes to provide class-specific meaning.

    This is the most common comparison, and thus the most fundamental place where you (as the author of a class) get to decide if two objects are "equal" or not.

    === — case equality

    For class Object, effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements.

    This is incredibly useful. Examples of things which have interesting === implementations:

    • Range
    • Regex
    • Proc (in Ruby 1.9)

    So you can do things like:

    case some_object
    when /a regex/
      # The regex matches
    when 2..4
      # some_object is in the range 2..4
    when lambda {|x| some_crazy_custom_predicate }
      # the lambda returned true
    end
    

    See my answer here for a neat example of how case+Regex can make code a lot cleaner. And of course, by providing your own === implementation, you can get custom case semantics.

    eql? — Hash equality

    The eql? method returns true if obj and other refer to the same hash key. This is used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses normally continue this tradition by aliasing eql? to their overridden == method, but there are exceptions. Numeric types, for example, perform type conversion across ==, but not across eql?, so:

    1 == 1.0     #=> true
    1.eql? 1.0   #=> false
    

    So you’re free to override this for your own uses, or you can override == and use alias :eql? :== so the two methods behave the same way.

    equal? — identity comparison

    Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the same object as b).

    This is effectively pointer comparison.

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

Sidebar

Related Questions

I am trying to understand difference between IQueryable, ICollection, IList & IDictionary interface which
I am trying to understand difference between IQueryable, ICollection, IList & IDictionary interface which
So I've been trying to better understand the difference between these two but all
I am trying to understand the difference between these two links: The first one
I am trying to understand difference between those two and really need a explanatory
I'm trying to understand the difference between sequences and lists. In F# there is
I'm trying to understand the difference between a squash and a rebase. As I
I am trying to understand the difference between this: if (isset($_POST['Submit'])) { //do something
I'm really trying to understand the difference between OpenID and OAuth? Maybe they're two
Been trying my best to understand this correctly. What is the difference between an

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.