pass reference of primitive data types to functions in ruby -
by default ruby passes copy of primitive values , references object types. how pass references of primitive
type variables (ex: integers, floating points) function?
ruby doesn't pass arguments reference:
def change(x) x = 2 # assigns local variable 'x' end = 1 change(a) #=> 1
you pass mutable object instead, e.g. hash "containing" integer:
def change(h) h[:x] = 2 end h = {x: 1} change(h) h[:x] #=> 2
Comments
Post a Comment