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

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top