No,
Itâs not strange in any way.
attr_reader simply gives you what java would call a âgetterâ method. A
means to access the variable. It says nothing about the status or
changeableness of the variable itself. Itâs about the attribute. Try
as you might, you will not be able to change the variable that is
assigned to a attr_reader specified attribute of an object.
For instance.
class Blah
def initialize(someval = âhiâ)
@yeah = someval
end
end
b = Blah.new
now, without opening up the class again, try to set the value of yeah
inside this instantiated Blah object (b).
You canât, becuase there is no âyeah=â method defined (manually, or by
attr_writer)
The object itself cannot be changed.
Now, if we open up the class again, and give it a reader method
class Blah
def yeah
@yeah
end
end
Now, try as we might again, we STILL cannot modify the object that it
points to.
HOWEVER, we CAN modify the object itself. This is implicit. Ruby is
VERY flexible.
For instance, if we wanted to we could do the following:
b.yeah[0] = âPâ
then accessing b.yeah will yield âPiâ
but if we query the object, itâs still the same object, itâs just been
modified. Note, though, that it is still the same object!
So, sending the â[]â message to yeah is not actually modifying any of
bâs data. Itâs modifying some of bâs ENCAPSULATED data, but not any of
bâs direct data.
This is a good thing. It goes hand in hand with dynamic typing (IMHO).
Ruby creates a world where it dares to give programmers the no-
illusions awareness that they have the freedom and power to do as they
please, and assumes they donât want to have any barriers to that
freedom and power.
It turns out that programmers never do stupid things simply because
they can, in much the same way that drivers of cars rarely crash cars
on purpose.
Rules come out of fear.
I point you towards the Tao te Ching, one of the eastâs oldest texts,
stanza 38.
- Degeneration
The man of superior character is not (conscious of his) character.
Hence he has character.
The man of inferior character (is intent on) not losing character.
Hence he is devoid of character.
The man of superior character never acts,
Nor ever (does so) with an ulterior motive.
The man of inferior character acts,
And (does so) with an ulterior motive.
The man of superior kindness acts,
But (does so) without an ulterior motive.
The man of superior justice acts,
And (does so) with an ulterior motive.
(But when) the man of superior li acts and finds no response,
He rolls up his sleeves to force it on others.
Therefore:
After Tao is lost, then (arises the doctrine of) humanity.
After humanity is lost, then (arises the doctrine of) justice.
After justice is lost, then (arises the doctrine of) li.
Now li is the thinning out of loyalty and honesty of heart.
And the beginning of chaos.
The prophets are the flowering of Tao
And the origin of folly.
Therefore the noble man dwells in the heavy (base),
And not in the thinning (end).
He dwells in the fruit,
And not in the flowering (expression).
Therefore he rejects the one and accepts the other.
In particular, after tao is lost, arises humanity, after humanity is
lost, arises justice.
Justice is rules. Now, do we need rules to bind us, or are we
enlightened enough to follow a path where no rules are necessary,
because we follow a way where our inner sense of naturalness and
beauty guides us on our way?
Julian.
Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/