Equality in Ruby

462 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
462
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Equality in Ruby

  1. 1. Equality inRubyRomain Sempé@rsempe
  2. 2. Equality or Identity?—  Equality: Different objects that represent the same value.—  Identity: Different variables that refer to the same object.
  3. 3. Equal yes! But in what sense?—  What equality methods should we use? —  == —  eql? —  equal? —  === —  =~
  4. 4. ==Test only if the objects have the same value.
  5. 5. eql?Test if the objects have the same value and the same type.
  6. 6. equal?Test if the objects have the same value, the same type and are the same instance in memory.
  7. 7. === Same as == but for case statements.One operator depending on the context of the comparison (Class, Range, Regexp, …).
  8. 8. =~Interesting with the Regexp class.It’s an alias to the match method.
  9. 9. 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?

×