private methods in Ruby

H

hemant

##Won't work method #1

class Foobar
private
def foo; p "Foobar"; end
end

class Baz < Foobar
def blah; foo; end
end

baz =3D Baz.new
baz.foo

## Work method #2

class Foobar
private
def foo; p "Foobar"; end
end

class Baz < Foobar
def blah; foo; end
end

baz =3D Baz.new
baz.blah

PickAxe2:

If a method is private, it may be called only within the context of
the calling object=97it is never possible to access another object's
private methods directly, even if the object is of the same class as
the caller.

So what happens in Method#2, why foo can be easily called, when called
from inside the class Baz? I mean, that method foo should be still
bound to instance baz...so in which context foo gets called in
Method#2.



--=20
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.
 
D

dblack

---2049402039-1599948326-1165880090=:15685
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1599948326-1165880090=:15685"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-1599948326-1165880090=:15685
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

##Won't work method #1

class Foobar
private
def foo; p "Foobar"; end
end

class Baz < Foobar
def blah; foo; end
end

baz =3D Baz.new
baz.foo

## Work method #2

class Foobar
private
def foo; p "Foobar"; end
end

class Baz < Foobar
def blah; foo; end
end

baz =3D Baz.new
baz.blah

PickAxe2:

If a method is private, it may be called only within the context of
the calling object=97it is never possible to access another object's
private methods directly, even if the object is of the same class as
the caller.

So what happens in Method#2, why foo can be easily called, when called
from inside the class Baz? I mean, that method foo should be still
bound to instance baz...so in which context foo gets called in
Method#2.

"In the context of" an object means: "when 'self' is that object". In
the first example, your call to foo takes place when baz is not self.
In the second example, inside blah, self is indeed baz, so it's
allowed to call foo.


David

--=20
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-1599948326-1165880090=:15685--
---2049402039-1599948326-1165880090=:15685--
 
H

hemant

Hi --



"In the context of" an object means: "when 'self' is that object". In
the first example, your call to foo takes place when baz is not self.
In the second example, inside blah, self is indeed baz, so it's
allowed to call foo.

So can i rephrase this as, private methods can't be called from
outside class definition, because self is not bound to anything
outside the class. If my assertion is correct, then ok..I get the
idea.

Thanks


--=20
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.
 
B

Brian C Broom

##Won't work method #1
=20
class Foobar
private
def foo; p "Foobar"; end
end
=20
class Baz < Foobar
def blah; foo; end
end
=20
baz =3D Baz.new
baz.foo
=20
## Work method #2
=20
class Foobar
private
def foo; p "Foobar"; end
end
=20
class Baz < Foobar
def blah; foo; end
end
=20
baz =3D Baz.new
baz.blah
=20
PickAxe2:
=20
If a method is private, it may be called only within the context of
the calling object=E2=80=94it is never possible to access another object'= s
private methods directly, even if the object is of the same class as
the caller.
=20
So what happens in Method#2, why foo can be easily called, when called
from inside the class Baz? I mean, that method foo should be still
bound to instance baz...so in which context foo gets called in
Method#2.
=20
I'll take a stab at this...

since the instance/object baz is of class Baz, inherited from Foobar, it
has the method foo. When you call baz.blah, baz internally calls its
own foo method, which it is allowed to do.=20

the "even if the object is of the same class..." bit is a little
confusing, but means that if you (somehow) link baz and baz2 (two
different instances) they can't call each others private methods, even
though they are the same class.

hope that helps

Brian Broom
 

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

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top