From: Xavier Noria Date: 2010-11-12T22:18:12+09:00 Subject: Re: what's an object? On Fri, Nov 12, 2010 at 1:28 PM, Josh Cheek wrote: > On Fri, Nov 12, 2010 at 5:38 AM, Xavier Noria wrote: > >> On Fri, Nov 12, 2010 at 12:25 PM, Josh Cheek wrote: >> >> The pointer value is copied into the number variable (pass by value). >> >> You can be certain the caller sees the exact same pointer when >> >> the function call returns. >> >> >> >> >> > Please re-read your blog with this in mind, since what you right here in >> > this quote are calling "pass by value" you, in your blog, call >> > "pass-by-reference". >> > >> > http://img193.imageshack.us/img193/6480/refl.jpg >> >> No, no. If Ruby did what the diagram shows the program >> >>    def m(b) >>      b = Object.new >>      p b.object_id >>    end >> >>    a = Object.new >>    m(a) >>    p a.object_id >> >> would print the same number because a and b would be pointing to the >> same storage area. They aren't. >> >> > How can b change what a points to when, according to your own diagram, b > does not have any reference to a? > > Here is the diagram which shows pass-by-reference and > pass-by-object-reference http://img405.imageshack.us/img405/3699/ref1.jpg > > And I think your confusion itself is adequate justification for Ruby's style > to deserve its own name "pass by object reference" > > Also, I would appreciate if more prominent Rubyists would weigh in on this. > So far, I am disagreeing with everyone in this thread. I would like to know > what the people that I have learned to respect think. I just disagree, > because those on the other side seem so conspicuously wrong. But the number > of people on that side seems disconcerting. I would like an objective > measure in the form of people whose conclusions I can't roll my eyes at. The Ruby spec says in section 6.2.1. "A variable is denoted by a name, and refers to an object, which is called the value of the variable. A variable itself is not an object. While a variable can refer to only one object at a time, an object can be referred to by more than one variable at a time." Then in section 13.3.3 you'll see that the spec says that you create local variable bindings from the arguments. For example, regarding mandatory parameters: "If Pi is a mandatory parameter, let n be the mandatory-parameter. If Pi is an optional parameter, let n be the optional-parameter-name. Create a variable binding with name n and value Ai in Sb." I admit that the term "reference" is not used. I admit also that the section about method invocation does not explicitly say "copy" as the JLS does. But a posteriori the semantics are pass by value. The binding is local, the variables are not aliases bound to the same storage area. When you assign to the local variable, the caller is unaffected. Being able to change the state of a mutable object is unrelated to call semantics. Conceptually my diagram lacks the middle boxes that represent the indirection between the variable and the objects themselves. I guess MRI binds VALUEs to variables, but I am only talking about semantics, about how this works conceptually, not about what a particular implementation does.