Ruby - passing by value or by reference -
i've searched on this, , seems ruby passes value, simple little example subset of larger program, , i'm little confused.
a = "j123" b = a.slice!(0) puts puts b
this displays
123 123
and don't understand why value of "b" changing.
because both a
, b
pointers refer same object. aren't object itself. can pass pointers arguments in ruby, , pointers passed value.
this similar java, javascript , python, , unlike c++, c#, haskell , scala.
the difference of interest in following example:
def f(y) y = 2 end x = 1 f(x) puts x # 1, not 2
if x
passed reference, print 2
.
Comments
Post a Comment