Last active
June 23, 2021 06:05
-
-
Save JuzerShakir/5b14a78163c8cfb1501dc536108270ab to your computer and use it in GitHub Desktop.
A look into object_id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Date Created: 17 June 2021 | |
# Ruby Version 3.0.0p | |
def object_id_of_str obj_1, obj_2 | |
# The argument has same object_id as original object because.. | |
# ..it refers to same object, it doesn't copy objects' value but it points.. | |
# ..to original objects' memory, hence holding objects' reference ID in the memory | |
puts "Before modification, object 1 ID: #{obj_1.object_id}" # => 60 | |
puts "Before modification, object 2 ID: #{obj_2.object_id}" # => 80 | |
# << operator changes value in the same memory location, modifying original obj | |
obj_1 << " there" | |
# same id | |
puts "After modification, object 1 ID: #{obj_1.object_id}" # => 60 | |
# = operator creates new object in memory | |
obj_2 = "TADA" | |
# since location is different in memory, id will be too | |
puts "After modification, object 2 ID: #{obj_2.object_id}" # => 100 | |
end | |
str_1 = "Hi" | |
str_2 = "Bye" | |
puts "Object ID of #{str_1}: #{str_1.object_id}" # => 60 | |
puts "Object ID of #{str_2}: #{str_2.object_id}" # => 80 | |
# calling function and passing above strings as arguments | |
object_id_of_str(str_1, str_2) | |
# AFTER Function call---- | |
# | |
# The modification of object in function happened to original object.. | |
# .. hence its value is now changed | |
puts "String 1 = #{str_1}" # => Hi there | |
# the original object does not get modified hence same value | |
puts "String 2 = #{str_2}" # => Bye |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def object_id_of_array obj | |
# original object id | |
puts "#{obj} = #{obj.object_id}" # => [1, 2, 3] = 60 | |
# .dup creates new objectwith different memeory address with same value as original obj | |
obj = obj.dup | |
# hence different id | |
puts "Duplicate object #{obj} = #{obj.object_id}" # => [1, 2, 3] = 80 | |
# appends new value | |
obj << "Extra" | |
puts "Duplicate object :#{obj}" # => [1, 2, 3, "Extra"] | |
# = operator cannot modify original obj because we are refering to different object and not original objects' id | |
obj = ["new value"] | |
puts "Duplicate object :#{obj}" # => ["new value"] | |
end | |
a = [1,2,3] | |
# calling function | |
object_id_of_array(a) | |
# original object isn't modified | |
puts "Original object: #{a}" # => [1, 2, 3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1,2,3] | |
puts "Original array id:#{a.object_id}" # => 60 | |
# an array within an array | |
nest = [a] | |
puts "nested array value:#{nest}" # => [[1, 2, 3]] | |
# different array ID | |
puts "nested array id:#{nest.object_id}" # => 80 | |
# inner array has same ID as Original array | |
puts "Inner array id:#{nest[0].object_id}" # => 60 | |
# duplicating nest array, which will give same value but different object_id | |
nest_dup = nest.dup | |
# different array ID | |
puts "duplicate array id:#{nest_dup.object_id}" # => 100 | |
# but not for inner array | |
puts "Inner duplicate array id:#{nest_dup[0].object_id}" # => 60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment