On 9/29/07, SpringFlowers AutumnMoon [email protected] wrote:
Austin Z. wrote:
A variable in Ruby isn’t like Java, PHP, C++, C, or even Pascal.
Doesn’t a Ruby variable behave the same way in Java, PHP5, C++, and
Python?
Didn’t I just say that they aren’t the same?
As long as I view Ruby as a pointer to an object, everything clicks.
But that’s exactly the WRONG way to look at it. In Pascal, Java,
C/C++, and possibly PHP5 (again, I don’t know Python’s semantics), a
variable takes up space.
In Ruby, a variable doesn’t contain anything. It doesn’t contain an
address, it doesn’t contain an object, it doesn’t contain anything. It’s
a label, a name, a sticky note attached to the object.
Forget C++, forget PHP. There are similarities, but you’re being
confused by concepts that have ZERO meaning in Ruby. So approach it from
a different direction.
I’ve seen you ask similar questions on three threads and you’re no
closer to understanding, which is why I’m telling you to forget what you
think you know.
Since Ruby variables aren’t containers (objects), they can’t be referred
to by other variables. In the code:
a = Dog.new(“phydeaux”)
a is not a pointer. Ever. You call it a pointer, and you’re going to
confuse people, especially yourself. What we say is that a is a
reference to “phydeaux”, or that it “refers to phydeaux”.
More often than not, though, we’ll be a bit smarter when we name our
variables:
class Dog
attr_accessor :name
def initialize(name)
@name = name
end
def woof
puts “Woof!”
end
end
def woof(dog)
dog.woof
end
phydeaux = Dog.new(“phydeaux”)
woof(phydeaux)
Seriously. Forget what you think you know. Because you’re not getting it
trying to relate it to anything else, and you’re just confusing the
discussion.
Ruby variables are called references. This is just a convention, but
it’s the prevailing convention. It’s not a pointer of any sort; it’s not
a C++ reference. This is because Ruby variables aren’t themselves
containers that can be referred to. It’s closest to Lisp bindings. If
that’s not clear, just remember that it has as much permanence as a
Post-It note.
-austin