Method behaviour

Discussion in 'Ruby' started by Sard Aukary, Aug 6, 2006.

  1. Sard Aukary

    Sard Aukary Guest

    Some methods like

    def change(word)
    word[0]='e'
    end
    a="affluent"
    puts a
    change(a)
    puts a

    alter the original object outside of the method, where as others don't.

    def change(word)
    word="fish"
    end
    a="cow"
    puts a
    change(a)
    puts a


    Why the change in behaviour, what determines it? Is the only way I can
    write methods that change the original object by using the Ruby methods
    that do this?

    --
    Posted via http://www.ruby-forum.com/.
     
    Sard Aukary, Aug 6, 2006
    #1
    1. Advertising

  2. On 06/08/06, Sard Aukary <> wrote:
    > Some methods like
    >
    > def change(word)
    > word[0]='e'
    > end
    > a="affluent"
    > puts a
    > change(a)
    > puts a
    >
    > alter the original object outside of the method, where as others don't.
    >
    > def change(word)
    > word="fish"
    > end
    > a="cow"
    > puts a
    > change(a)
    > puts a
    >
    >
    > Why the change in behaviour, what determines it? Is the only way I can
    > write methods that change the original object by using the Ruby methods
    > that do this?
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    In the second example you are changing the reference to where 'word'
    points. You create a new string ('fish') and then set 'word' to
    reference it. 'a' still points to your original word ('cow').
     
    Farrel Lifson, Aug 6, 2006
    #2
    1. Advertising

  3. Sard Aukary

    Guest

    Hi --

    On Mon, 7 Aug 2006, Sard Aukary wrote:

    > Some methods like
    >
    > def change(word)
    > word[0]='e'
    > end
    > a="affluent"
    > puts a
    > change(a)
    > puts a
    >
    > alter the original object outside of the method, where as others don't.
    >
    > def change(word)
    > word="fish"
    > end
    > a="cow"
    > puts a
    > change(a)
    > puts a
    >
    >
    > Why the change in behaviour, what determines it? Is the only way I can
    > write methods that change the original object by using the Ruby methods
    > that do this?


    It's because there's a distinction between (a) sending a message to an
    object, and (b) assigning to a variable.

    a = "cow" # assignment
    a[0] = 'w' # send message "[]" to a
    a = "fish" # assignment -- reusing the identifier 'a'

    Whenever you assign to a variable, you're wiping the slate clean: the
    variable loses all association with the object it referred to
    previously (if any).

    The "word[0] = 'e'" expression looks like assignment, but technically
    it's calling the method "[]=" on the object word, with the single
    argument 'e'. It's not the same as just plain assignment to an
    identifier.


    David

    --
    http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
    ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
    http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
    http://www.manning.com/black => book, Ruby for Rails
    http://www.rubycentral.org => Ruby Central, Inc.
     
    , Aug 6, 2006
    #3
  4. Your assessment is correct but you got one detail wrong. The message
    sent by

    word[0]='e'

    is actually []= with arguments 0 and 'e'. That is, the pseudo-
    assignment is really the message

    word.[]=(0, 'e')

    As a coating of syntactic sugar, Ruby permits us to write this as
    something that looks like an assignment and which, consequently, can
    confuse people like the OP.

    Regards, Morton

    P.S. Don't think I'm complaining about the syntactic sugar: the real
    message underlying the sugar coating is ugly. I don't want to write
    stuff like that.

    On Aug 6, 2006, at 4:48 PM, wrote:

    > Hi --
    >
    > On Mon, 7 Aug 2006, Sard Aukary wrote:
    >
    >> Some methods like
    >>
    >> def change(word)
    >> word[0]='e'
    >> end
    >> a="affluent"
    >> puts a
    >> change(a)
    >> puts a
    >>
    >> alter the original object outside of the method, where as others
    >> don't.
    >>
    >> def change(word)
    >> word="fish"
    >> end
    >> a="cow"
    >> puts a
    >> change(a)
    >> puts a
    >>
    >>
    >> Why the change in behaviour, what determines it? Is the only way
    >> I can
    >> write methods that change the original object by using the Ruby
    >> methods
    >> that do this?

    >
    > It's because there's a distinction between (a) sending a message to an
    > object, and (b) assigning to a variable.
    >
    > a = "cow" # assignment
    > a[0] = 'w' # send message "[]" to a
    > a = "fish" # assignment -- reusing the identifier 'a'
    >
    > Whenever you assign to a variable, you're wiping the slate clean: the
    > variable loses all association with the object it referred to
    > previously (if any).
    >
    > The "word[0] = 'e'" expression looks like assignment, but technically
    > it's calling the method "[]=" on the object word, with the single
    > argument 'e'. It's not the same as just plain assignment to an
    > identifier.
     
    Morton Goldberg, Aug 7, 2006
    #4
  5. Sard Aukary

    Guest

    Hi --

    On Mon, 7 Aug 2006, Morton Goldberg wrote:

    > Your assessment is correct but you got one detail wrong. The message sent by
    >
    > word[0]='e'
    >
    > is actually []= with arguments 0 and 'e'.


    Whoops, right: two arguments.


    David

    --
    http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
    ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
    http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
    http://www.manning.com/black => book, Ruby for Rails
    http://www.rubycentral.org => Ruby Central, Inc.
     
    , Aug 7, 2006
    #5
    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. Tom
    Replies:
    1
    Views:
    8,354
    Paul Tomblin
    Jun 27, 2003
  2. Batista, Facundo

    Standard behaviour of a getSomething method

    Batista, Facundo, Jul 23, 2003, in forum: Python
    Replies:
    2
    Views:
    297
    Dan Williams
    Jul 24, 2003
  3. Batista, Facundo

    Standard behaviour of a getSomething method

    Batista, Facundo, Jul 24, 2003, in forum: Python
    Replies:
    0
    Views:
    270
    Batista, Facundo
    Jul 24, 2003
  4. Andy Chambers
    Replies:
    1
    Views:
    396
    Daniel Dyer
    May 14, 2007
  5. Kyung won Cheon
    Replies:
    0
    Views:
    221
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page