Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2015
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: basic ENCAPSULATION help |
| Date | 2011-03-30 21:20 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <aeb3fb3bb857ee7cfd4891ee46acfb11@ruby-forum.com> (permalink) |
| References | <7ae35890c4342a71e3467ade701d3556@ruby-forum.com> |
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.
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
basic ENCAPSULATION help Kaye Ng <sbstn26@yahoo.com> - 2011-03-30 04:33 -0500
Re: basic ENCAPSULATION help Gunther Diemant <g.diemant@gmx.net> - 2011-03-30 04:45 -0500
Re: basic ENCAPSULATION help 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 21:20 -0500
Re: basic ENCAPSULATION help 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 21:37 -0500
csiph-web