basic ENCAPSULATION help

K

Kaye Ng

This example demonstrates the use of 'private'

class Person
def initialize(name)
set_name(name)
end
def name
@first_name + ' ' + @last_name
end
private
def set_name(name)
first_name, last_name =3D name.split(/\s+/)
set_first_name(first_name)
set_last_name(last_name)
end
def set_first_name(name)
@first_name =3D name
end
def set_last_name(name)
@last_name =3D name
end
end

From the book:
" private tells Ruby that any methods declared in this class from
there on should be kept private. This means that only code within the
object=E2=80=99s methods can
access those private methods, whereas code outside of the class cannot.
For example, this code no longer works "

p =3D Person.new("Fred Bloggs")
p.set_last_name("Smith")
_________________________________________________________________________=
_
NoMethodError: private method 'set_last_name' called for
#<Person:0x337b68
@last_name=3D"Bloggs", @first_name=3D"Fred">
_________________________________________________________________________=
__

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" ?

That's it for now, more questions later. Apologies to the author of
this book, it's not you, it's me. I'm a really slow learner.

Thanks guys!

-- =

Posted via http://www.ruby-forum.com/.=
 
G

Gunther Diemant

In your example: in the initialize method you call the private method
set_name (with no error). That is calling the code from inside the class.
p =3D Person.new
p.set_name('foo')
raises an error. That is calling the code from outside the class.

Hope that helps.
 
7

7stud --

One of the key aspects of a private method is: a private method cannot
be called with an explicit "receiver". What does that mean? The
receiver is the object calling the method. In the following example:


class Dog
def bark
puts "woof"
end
end

spot = Dog.new
spot.bark

--output:--
woof

...spot is the "receiver" and spot calls the method bark(). However,
according to the rules of ruby, you cannot specify a receiver when you
call a private method.

Well, than how does ruby know which object is calling the method? Now
you enter the tricky realm of what's known as 'self'. When a method is
not called with a receiver, ruby implicitly uses whatever object is self
at the instant the method is called.

RULE #1: When ruby executes code inside a method, then inside the method
self is equal to the object that called the method. So, for instance,
in the example above, when spot calls bark(), inside bark(), self is
equal to spot.

What that implies is that you will usually call private methods from
inside public methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top