Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Equality in Ruby

Equality in Ruby

Romain Sempé

August 14, 2012
Tweet

More Decks by Romain Sempé

Other Decks in Programming

Transcript

  1. Equality or Identity? —  Equality: Different objects that represent the

    same value. —  Identity: Different variables that refer to the same object.
  2. Equal yes! But in what sense? —  What equality methods

    should we use? —  == —  eql? —  equal? —  === —  =~
  3. equal? Test if the objects have the same value, the

    same type and are the same instance in memory.
  4. === Same as == but for case statements. One operator

    depending on the context of the comparison (Class, Range, Regexp, …).
  5. Conclusion —  == is great most of the time — 

    eql? and equal? can be useful to be more precise —  Elegant case statements with === —  Concise test for Regexp with =~ —  Only one identity operator: equal?