Pointers in Ruby
Ruby doesn’t have explicit pointers like Go, but we can demonstrate similar concepts using object references and mutable objects.
In this Ruby version, zeroval
doesn’t change the i
in the main program, but zeroref
does because it has a reference to the mutable array that contains our value.
Ruby uses object references instead of pointers. When you pass an object to a method, you’re passing a reference to that object. For immutable objects like numbers, this behaves like pass-by-value. For mutable objects like arrays, it behaves more like pass-by-reference.
The object_id
in Ruby is somewhat analogous to a memory address in languages with explicit pointers. It’s a unique identifier for each object, but it’s not a direct memory address that you can manipulate like in lower-level languages.
This example demonstrates how Ruby handles value types and reference types differently, which is conceptually similar to the distinction between values and pointers in languages like Go.