How to use a passing argument(returned argument)?

Discussion in 'Ruby' started by ±è ÁØ¿µ, Nov 26, 2008.

  1. 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?
     
    ±è ÁØ¿µ, Nov 26, 2008
    #1
    1. Advertising

  2. ±è ÁØ¿µ

    Eric I. Guest

    On Nov 25, 8:49 pm, ±è ÁØ¿µ <> wrote:
    > 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
     
    Eric I., Nov 26, 2008
    #2
    1. Advertising

  3. On Nov 25, 2008, at 8:49 PM, =EA=B9=80 =EC=A4=80=EC=98=81 wrote:

    > 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
     
    Joshua Ballanco, Nov 26, 2008
    #3
  4. On 26.11.2008, at 04:19 , Joshua Ballanco wrote:

    > On Nov 25, 2008, at 8:49 PM, =EA=B9=80 =EC=A4=80=EC=98=81 wrote:
    >
    >> 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-=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
     
    Einar Magnús Boson, Nov 26, 2008
    #4
  5. ±è ÁØ¿µ

    Junyoung Kim Guest

    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:

    >
    > On 26.11.2008, at 04:19 , Joshua Ballanco wrote:
    >
    >> On Nov 25, 2008, at 8:49 PM, =EA=B9=80 =EC=A4=80=EC=98=81 wrote:
    >>
    >>> Hello, everyone !!!

    >>
    >> Hi there!
    >>
    >>> I have question about how to use a passing argument. firstly let =20
    >>> me 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-=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 =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=


    > 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
    >
    >
    >
    >
     
    Junyoung Kim, Nov 26, 2008
    #5
  6. ±è ÁØ¿µ

    Ken Bloom Guest

    On Tue, 25 Nov 2008 23:38:02 -0500, Einar Magnús Boson wrote:
    >
    > 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)

    --
    Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
     
    Ken Bloom, Nov 27, 2008
    #6
  7. > a = 22
    > b = 33
    > swap(ref{:a}, ref{:b})
    > p a # => 33
    > p b # => 22



    instead of swap(ref{:a}, ref{:b})

    why not a, b = b, a
    --
    Posted via http://www.ruby-forum.com/.
     
    Lloyd Linklater, Nov 27, 2008
    #7
  8. On 27.11.2008, at 03:06 , Lloyd Linklater wrote:

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

    >
    >
    > instead of swap(ref{:a}, ref{:b})
    >
    > why not a, b = b, a
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    Because his question was about references, swap was just an example.

    einarmagnus
     
    Einar Magnús Boson, Nov 27, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Borse, Ganesh
    Replies:
    0
    Views:
    307
    Borse, Ganesh
    Nov 14, 2007
  2. Muddy Coder
    Replies:
    1
    Views:
    195
  3. Sam Sungshik Kong

    How can I use the last returned value?

    Sam Sungshik Kong, Jun 8, 2004, in forum: Ruby
    Replies:
    6
    Views:
    132
  4. Trans
    Replies:
    3
    Views:
    208
    Robert Klemme
    Jan 26, 2005
  5. User1014
    Replies:
    1
    Views:
    200
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page