--rJwd6BRFiFCcLxzm
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Mar 30, 2011 at 06:33:00PM +0900, Kaye Ng wrote:
>=20
> When the author says, "only code within the object=E2=80=99s methods can
> access those private methods, whereas code outside of the class
> cannot.", what is the code in the example that can "access those private
> methods", and what is the "code outside of the class that cannot" ?

To expand upon what Gunther Diemant said:

The initialize method calls the set_name method.  Both of these are
within the Person class, so when initialize calls set_name, the private
method set_name is being called by another method within the class.  The
set_name method then calls the set_first_name and set_last_name methods.
These are two more cases of private methods (set_first_name and
set_last_name) being called by other code (set_name) inside the Person
class.

Code outside the method that would not be able to call the private
methods might include something like this:

    monickers =3D ['Alan','Bob','Charlie']
    johnson =3D Person.new('Dave Johnson')
   =20
    monickers.each do |fname|
      johnson.set_first_name fname
      puts johnson.name
    end

The expected output, if the set_first_name method was not private, would
be something like this:

    Alan Johnson
    Bob Johnson
    Charlie Johnson

Because set_first_name is a private method, however, the monickers.each
block fails to access it, resulting in output that looks more like this:

    NoMethodError: private method `set_first_name' called for
    #<Person:0x7f4f1457c5c8 @last_name=3D"Johnson", @first_name=3D"Dave">

As you can see, setting those methods to "private" results in an error,
because they are then protected from access by messages sent to the
object from outside of the object.

--=20
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

--rJwd6BRFiFCcLxzm
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iEYEARECAAYFAk2TQ6gACgkQ9mn/Pj01uKV2ywCgsJ8aqiDcyde9v2iAkvAQEAlN
lfMAoNNsHZ28/ir7sqlCua5W+ADvjE3a
=iiXM
-----END PGP SIGNATURE-----

--rJwd6BRFiFCcLxzm--

On Wed, Mar 30, 2011 at 06:33:00PM +0900, Kaye Ng wrote:
> 
> When the author says, "only code within the object°«s methods can
> access those private methods, whereas code outside of the class
> cannot.", what is the code in the example that can "access those private
> methods", and what is the "code outside of the class that cannot" ?

To expand upon what Gunther Diemant said:

The initialize method calls the set_name method.  Both of these are
within the Person class, so when initialize calls set_name, the private
method set_name is being called by another method within the class.  The
set_name method then calls the set_first_name and set_last_name methods.
These are two more cases of private methods (set_first_name and
set_last_name) being called by other code (set_name) inside the Person
class.

Code outside the method that would not be able to call the private
methods might include something like this:

    monickers = ['Alan','Bob','Charlie']
    johnson = Person.new('Dave Johnson')
    
    monickers.each do |fname|
      johnson.set_first_name fname
      puts johnson.name
    end

The expected output, if the set_first_name method was not private, would
be something like this:

    Alan Johnson
    Bob Johnson
    Charlie Johnson

Because set_first_name is a private method, however, the monickers.each
block fails to access it, resulting in output that looks more like this:

    NoMethodError: private method `set_first_name' called for
    #<Person:0x7f4f1457c5c8 @last_name="Johnson", @first_name="Dave">

As you can see, setting those methods to "private" results in an error,
because they are then protected from access by messages sent to the
object from outside of the object.

-- 
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iEYEARECAAYFAk2TQ6gACgkQ9mn/Pj01uKV2ywCgsJ8aqiDcyde9v2iAkvAQEAlN
lfMAoNNsHZ28/ir7sqlCua5W+ADvjE3a
=iiXM
-----END PGP SIGNATURE-----