string = "some string" same_string = string copy_string = "some string" int = 7 same_int = int copy_float = 7.0 new_int = 7 class TestClass def initialize(content) @content = content end end class SubTestClass < TestClass def initialize(content) super(content) end end test_obj = TestClass.new("something") same_obj = test_obj new_obj = TestClass.new("something") sub_obj = SubTestClass.new("something")
puts string.equal?(same_string) #true, , puts string.equal?(copy_string) #false, puts int.equal?(same_int) #true, puts int.equal?(copy_float) #false 7 7.0 - puts int.equal?(new_int) #true, Fixnum , # object_id . , , new_int, int puts test_obj.equal?(same_obj) #true, puts test_obj.equal?(new_obj) #false, , puts test_obj.equal?(sub_obj) #false, ,
puts string.eql?(same_string) #true, , puts string.eql?(copy_string) #true, - puts int.eql?(same_int) #true, equal? , eql? puts int.eql?(copy_float) #false, , puts int.eql?(new_int) #true, , , puts test_obj.eql?(same_obj) #true, , puts test_obj.eql?(new_obj) #false, !, eql? Object, # equal? puts test_obj.eql?(sub_obj) #false, , ,
puts string == (same_string) #true, puts string == (copy_string) #true, puts int == (same_int) #true, puts int == (copy_float) #true, - , puts int == (new_int) #true, #! == , #Object, equal? puts test_obj == (same_obj) #true puts test_obj == (new_obj) #false puts test_obj == (sub_obj) #false
puts /p.*cock/ == 'peacock' #false, == , puts /p.*cock/ === 'peacock' #true, ===
Source: https://habr.com/ru/post/124113/
All Articles