Variables in Ruby are basically pointers to an object in memory (heap).
a = "bacon" puts "a's object id: #{a.object_id}" # b points to the same Object as a b = a puts "b's object id: #{b.object_id}" # a's object id: 70226414807760 # b's object id: 70226414807760 a[2] = "k" puts "a's value: #{a}" puts "b's value: #{b}" # a's value: bakon # b's value: bakon # This reassigns a to a new String a = "cheese" puts "a's new object id: #{a.object_id}" puts "a's new value: #{a}" # a's new object id: 70226414806980 # a's new value: cheese puts "b's same value: #{b}" puts "b has the same object id: #{b.object_id}" # b's same value: bakon # b has the same object id: 70226414807760
Hopefully this illustrates how variables work in Ruby.
Hi,
good synthesis of variables basics ! thanks
and this format is particulary nice (code + console output)
regards