insteresting thing about return results from function arguments

U

uncutstone

Please take a look at following code and its result.

def geta(a)
a<<1<<2<<3<<4
end

def getb(b)
b=[7,8,9,10]
end

a=[]
b=[]
geta(a)
getb(b)
puts a.inspect
puts b.inspect

the result is:

[1, 2, 3, 4]
[]
 
G

gabriele renzi

uncutstone ha scritto:
Please take a look at following code and its result.

def geta(a)
a<<1<<2<<3<<4
end

here you're changing the argument (side effet)
def getb(b)
b=[7,8,9,10]
end

here you're just changing the local variable, thus
a=[]
b=[]
geta(a) # changing the empty array referenced by "a"
getb(b) # leaving the empty array happily alone
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top