How to use a passing argument(returned argument)?

±

±è ÁØ¿µ

Hello, everyone !!!

I have question about how to use a passing argument. firstly let me
show sample code.

a = 0

def set10(aArg)
aArg = 10
end

set10(a)

p a <--- I wanna get 10 as a result.


How can I do for this one?
 
E

Eric I.

Hello, everyone !!!

I have question about how to use a passing argument. firstly let me
show sample code.

a = 0

def set10(aArg)
aArg = 10
end

set10(a)

p a <--- I wanna get 10 as a result.

How can I do for this one?

Well, Ruby, being a purely object-oriented language, means that
assigning a value to a variable assigns it to a new object (instance)
without affecting the original. So in your example, you just make the
parameter aArg refer to a new number leaving the original untouched.

To alter the data in a parameter, you would have to call a method on
the parameter that changes the data. For example:

----

def method1(string)
string.upcase!
end

s = "hello"
method1(s)
p s # displays "HELLO"

----

Now, some classes have immutable instances -- instances that cannot
have the data within them altered. Fixnum, Bignum, and Float (the
basic numeric types) are all immutable. So that presents a problem.

What you could do is wrap the immutable instance in a mutable class,
as in:

----

require 'delegate'

class Mutable < SimpleDelegator
def initialize(value)
@value = value
super(@value)
end

def reassign(new_value)
@value = new_value
__setobj__(@value)
end
end

def method2(number)
number.reassign(10)
end

n = Mutable.new(3)
method2(n)
p n # displays 10

n = Mutable.new("test")
p n # displays "test"
method2(n)
p n # displays 10

----

I hope that's helpful.

Eric

====

Are you interested in on-site Ruby or Ruby on Rails training
that uses well-designed, real-world, hands-on exercises?
http://LearnRuby.com
 
J

Joshua Ballanco

Hello, everyone !!!

Hi there!
I have question about how to use a passing argument. firstly let me =20=
show sample code.

a =3D 0

def set10(aArg)
aArg =3D 10
end

set10(a)

p a <--- I wanna get 10 as a result.


How can I do for this one?

First, the pattern you are looking for is not very object oriented-y. =20=

That is, you're asking to remove some of the logic pertaining to 'a' =20
and put it somewhere outside of 'a'. Since 'a' is an object =20
(everything in Ruby is), then it would be best if you made 'a' an =20
instance of a class with the 'set10' logic in it. The sort of pass-by-=20=

reference pattern you're looking for is much more typical of C.

That said, this would be, I think, the closest equivalent in Ruby:

a =3D 0

def set10(aArg_name, bind)
eval("#{aArg_name} =3D 10", bind)
end

set10('a', binding)

p a

=3D> 10

Cheers,

Josh
 
E

Einar Magnús Boson

Hi there!


First, the pattern you are looking for is not very object oriented-=20
y. That is, you're asking to remove some of the logic pertaining to =20=
'a' and put it somewhere outside of 'a'. Since 'a' is an object =20
(everything in Ruby is), then it would be best if you made 'a' an =20
instance of a class with the 'set10' logic in it. The sort of pass-=20
by-reference pattern you're looking for is much more typical of C.

That said, this would be, I think, the closest equivalent in Ruby:

a =3D 0

def set10(aArg_name, bind)
eval("#{aArg_name} =3D 10", bind)
end

set10('a', binding)

p a

=3D> 10

Cheers,

Josh


I did not know this, google filled me in and this might be relevant to =20=

the original question:
http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print

They end up with this:

def swap(aref, bref)
aref.value, bref.value =3D bref.value, aref.value
end

a =3D 22
b =3D 33
swap(ref{:a}, ref{:b})
p a # =3D> 33
p b # =3D> 22

Pretty neat, if that's something you wanna do. Makes me wonder though: =20=

when I first saw this (5 minutes ago) I thought you might be able to do

a =3D 0

def set10(aArg_name, bind=3Dbinding)
eval("#{aArg_name} =3D 10", bind)
end

set10('a')

p a


but that doesn't work, in what context and when are default-values =20
evaluated? my guess is that they get the same closure as the method =20
body and are evaluated on call, is that correct?

einarmagnus
 
J

Junyoung Kim

Einar's suggestion is very good and clear everything for me :)

finally, my code should be changed like -->

class Reference
def initialize(var_name, vars)
@getter =3D eval "lambda { #{var_name} }", vars
@setter =3D eval "lambda { |v| #{var_name} =3D v }", vars
end
def value
@getter.call
end
def value=3D(new_value)
@setter.call(new_value)
end
end

def ref(&block)
Reference.new(block.call, block.binding)
end

def set10(var_a)
var_a.value =3D 10
end

a =3D 22
set10 (ref{:a})
p a

thanks for all :)

2008. 11. 26, =EC=98=A4=ED=9B=84 1:38, Einar Magn=C3=BAs Boson =EC=9E=91=EC=
=84=B1:
I did not know this, google filled me in and this might be relevant =20=
to the original question:
= http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print

They end up with this:

def swap(aref, bref)
aref.value, bref.value =3D bref.value, aref.value
end

a =3D 22
b =3D 33
swap(ref{:a}, ref{:b})
p a # =3D> 33
p b # =3D> 22

Pretty neat, if that's something you wanna do. Makes me wonder =20
though: when I first saw this (5 minutes ago) I thought you might be =20=
 
K

Ken Bloom

I did not know this, google filled me in and this might be relevant to
the original question:
http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print

They end up with this:

def swap(aref, bref)
aref.value, bref.value = bref.value, aref.value end

a = 22
b = 33
swap(ref{:a}, ref{:b})
p a # => 33
p b # => 22

Pretty neat, if that's something you wanna do.

That rocks. Thanks for sharing. I thought that this kind of functionality
would require syntax built into the language.
Makes me wonder though:
when I first saw this (5 minutes ago) I thought you might be able to do

a = 0

def set10(aArg_name, bind=binding)
eval("#{aArg_name} = 10", bind)
end

set10('a')

p a


but that doesn't work, in what context and when are default-values
evaluated? my guess is that they get the same closure as the method body
and are evaluated on call, is that correct?

Yeah. I'd love to have a Binding.of_caller or something like that, but
have had no such luck. (Ok. It's an add-on in 1.8, but I'm curious about
1.9)
 

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
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top