Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #1950 > unrolled thread
| Started by | Kaye Ng <sbstn26@yahoo.com> |
|---|---|
| First post | 2011-03-30 04:33 -0500 |
| Last post | 2011-03-30 21:37 -0500 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.ruby
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
| From | Kaye Ng <sbstn26@yahoo.com> |
|---|---|
| Date | 2011-03-30 04:33 -0500 |
| Subject | basic ENCAPSULATION help |
| Message-ID | <7ae35890c4342a71e3467ade701d3556@ruby-forum.com> |
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 = name.split(/\s+/)
set_first_name(first_name)
set_last_name(last_name)
end
def set_first_name(name)
@first_name = name
end
def set_last_name(name)
@last_name = 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’s methods can
access those private methods, whereas code outside of the class cannot.
For example, this code no longer works "
p = Person.new("Fred Bloggs")
p.set_last_name("Smith")
__________________________________________________________________________
NoMethodError: private method 'set_last_name' called for
#<Person:0x337b68
@last_name="Bloggs", @first_name="Fred">
___________________________________________________________________________
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" ?
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/.
[toc] | [next] | [standalone]
| From | Gunther Diemant <g.diemant@gmx.net> |
|---|---|
| Date | 2011-03-30 04:45 -0500 |
| Message-ID | <AANLkTimK=dXutgg2M3Ot9EcY5=HwOag+LLvhEAnGn4Pg@mail.gmail.com> |
| In reply to | #1950 |
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 = Person.new
p.set_name('foo')
raises an error. That is calling the code from outside the class.
Hope that helps.
--
gd
2011/3/30 Kaye Ng <sbstn26@yahoo.com>
> 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 = name.split(/\s+/)
> set_first_name(first_name)
> set_last_name(last_name)
> end
> def set_first_name(name)
> @first_name = name
> end
> def set_last_name(name)
> @last_name = 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’s methods can
> access those private methods, whereas code outside of the class cannot.
> For example, this code no longer works "
>
> p = Person.new("Fred Bloggs")
> p.set_last_name("Smith")
> __________________________________________________________________________
> NoMethodError: private method 'set_last_name' called for
> #<Person:0x337b68
> @last_name="Bloggs", @first_name="Fred">
> ___________________________________________________________________________
>
> 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" ?
>
> 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/.
>
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-03-30 21:20 -0500 |
| Message-ID | <aeb3fb3bb857ee7cfd4891ee46acfb11@ruby-forum.com> |
| In reply to | #1950 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-03-30 21:37 -0500 |
| Message-ID | <2f1af9397f90d3fd1b36328dda6a0764@ruby-forum.com> |
| In reply to | #2015 |
7stud -- wrote in post #990108: > because RULE #1 says you cannot use an explicit receiver to call a > private method. > Whoops. That's not RULE #1. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web