Jean-Hugues ROBERT said:
I don't agree. I don't think that an object IS a value.
An object is more than a value, it is made of:
an identity
a value (or state, or attributes, or ...)
a behavior
Well... that's the way I learned OO.
Hm, I think we use a different notion of "value" here. I'd say that in
Ruby every value is an object - as opposed to Java for example, where
there is a distinction between POD's (int, char, long...) and objects.
Two objects can sometime have the same "value", this
is how == compares for equality.
#== and #equal? are completely independent of values: they form a
convention about when two instances are considered equivalent. For class
Object they have to share the same identity, i.e., every instance is
equivalent to itself only. For other classes (take structs as an example)
equivalence is defined recursively via equivalence of instance variables.
When I draw a line between "object" and "value" it is
about those special objects that compare equal to themselves
only. These are always immutable objects.
That last statement is not true:
class Foo
attr_accessor :bar
end
Instances of Foo will only be equal to themselfs but they are not
immutable. This is because the inherit default behavior of Object.
Neither immutablility nor the definition of equivalence of Fixnums make
them special. What makes Fixnum special is the way those instances are
created and treated internally in the Ruby interpreter (which is merely an
implementation detail from the perspective of the language). But
otherwise they are completely normal - there is no such concept as in
Java, where you can have objects and POD's. Consider this:
class Fixnum
def eql?(x) false end
def ==(x) false end
end
=> false
You can change the definition of Fixnum equivalence at your discretion.
Of course, many things would break if you do, but that's another story.
For example,
no two objects can have the same value 3, because 3
is an immutable integer. The same is true for true, false,
nil. On the other hand, two different strings can both
be valued "3". So, in my mind, 3 is a value, whereas
"3" is an object (whose initial value is "3"). To me
values are objects with some semantic restrictions.
Don't let yourself be fooled by the syntax: the char sequence '3' refers
an object that, in numerical calculations, behaves like the number 3 (i.e.
the mathematical concept). The char sequence '"3"' referns an object that
contains the given characters literally. Again, from the perspective of
the language Ruby, it's an implementation detail, that the object
accessible via '3' is always the same:
=> 7
while it's not for '"3"':
=> 134969516
Of course it makes good sense to make the instance immutable so nobody can
change the state of the instance that the literal 3 refers. You can treat
the object accessible via '3' much the same like others although there are
some restrictions due to the implementation:
=> Fixnum
But then: nil.true? => nil; I was expecting true, xxx? methods should
return a boolean I think.
That's not necessary in Ruby because all values can be treated
meaningfully in a boolean context (as opposed to Java for example).
I don't. But the original poster may have a different answer I guess.
Yeah, probably.
I suspect that this is less efficient. My
verbose code leverage the efficient method
dispatching of the interpreter (versus the
additional === test).
You're definitely right here.
Should I open up a savings account?
Unfortunately I have to go now, otherwise my answer would have been more
verbose, I guess. But we can continue this tomorrow.
Kind regards
robert