confused about access control 
Author Message
 confused about access control

Hi,

I'm rather confused about access control. Observe this:

irb(main):001:0> class String
irb(main):002:1>   protected
irb(main):003:1>   def undot!
irb(main):004:2>     sub!(/^\.\./, '.')
irb(main):005:2>   end
irb(main):006:1> end
nil
irb(main):007:0> class Foo
irb(main):008:1>   foo = '..123'
irb(main):009:1>   foo.undot!
irb(main):010:1> end
NameError: protected method `undot!' called for "..123":String
        from (irb):9

Why does this fail?

The pick-axe book says:

"Protected methods can be invoked only by objects of the defining
class and its subclasses."

Well, foo is an instance of String, which is the class that defines
the undot! method, so why does this not work?

I'm clearly missing something obvious.

Ian
--
Ian Macdonald               | Fourth Law of Thermodynamics:  If the

                            | it is damn near zero.   -- David Ellis
                            |
                            |



Mon, 08 Nov 2004 07:48:48 GMT  
 confused about access control

Quote:

> Hi,

> I'm rather confused about access control. Observe this:

> irb(main):001:0> class String
> irb(main):002:1>   protected
> irb(main):003:1>   def undot!
> irb(main):004:2>     sub!(/^\.\./, '.')
> irb(main):005:2>   end
> irb(main):006:1> end
> nil
> irb(main):007:0> class Foo
> irb(main):008:1>   foo = '..123'
> irb(main):009:1>   foo.undot!
> irb(main):010:1> end
> NameError: protected method `undot!' called for "..123":String
>         from (irb):9

> Why does this fail?

> The pick-axe book says:

> "Protected methods can be invoked only by objects of the defining
> class and its subclasses."

> Well, foo is an instance of String, which is the class that defines
> the undot! method, so why does this not work?

foo is an instance of String, but Foo isn't a subclass of String.
Even if Foo were a subclass of String, foo.undot! is trying to publically
access the undot! method.

class Bar < String
  def undot_twice
    self.undot!.undot!
  end  
end

bar = Bar.new('....123')
bar.undot_twice => '123'

--
Alan Chen
Digikata LLC
http://digikata.com



Mon, 08 Nov 2004 08:25:17 GMT  
 confused about access control

Quote:

> > Hi,

> > I'm rather confused about access control. Observe this:

> > irb(main):001:0> class String
> > irb(main):002:1>   protected
> > irb(main):003:1>   def undot!
> > irb(main):004:2>     sub!(/^\.\./, '.')
> > irb(main):005:2>   end
> > irb(main):006:1> end
> > nil
> > irb(main):007:0> class Foo
> > irb(main):008:1>   foo = '..123'
> > irb(main):009:1>   foo.undot!
> > irb(main):010:1> end
> > NameError: protected method `undot!' called for "..123":String
> >         from (irb):9

> > Why does this fail?

> > The pick-axe book says:

> > "Protected methods can be invoked only by objects of the defining
> > class and its subclasses."

> > Well, foo is an instance of String, which is the class that defines
> > the undot! method, so why does this not work?

> foo is an instance of String, but Foo isn't a subclass of String.
> Even if Foo were a subclass of String, foo.undot! is trying to publically
> access the undot! method.

foo (lowercase) IS a[n instance of] String, right?  So why can't an instance of
String call a protected method of String?  I don't think the enclosing "Foo"
class definition is relevant here, is it?


Mon, 08 Nov 2004 11:02:24 GMT  
 confused about access control

Quote:
> > > "Protected methods can be invoked only by objects of the defining
> > > class and its subclasses."

> > > Well, foo is an instance of String, which is the class that defines
> > > the undot! method, so why does this not work?

> > foo is an instance of String, but Foo isn't a subclass of String.
> > Even if Foo were a subclass of String, foo.undot! is trying to publically
> > access the undot! method.

> foo (lowercase) IS a[n instance of] String, right?  So why can't an
> instance of String call a protected method of String?  I don't think
> the enclosing "Foo" class definition is relevant here, is it?

Stop saying "call" and instead say "send a message" because that's
where the confusion arises.

An object will only receive messages for protected methods from
objects of the same type or its decendants.

In the code example, the sending object is NOT an instance of
String, it's an instance of Foo which is not an instance of
String or a descendant of String.

-- Dossy

--

Panoptic Computer Network             web: http://www.panoptic.com/
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)



Mon, 08 Nov 2004 11:07:39 GMT  
 confused about access control

Quote:

> foo (lowercase) IS a[n instance of] String, right?  So why can't an
> instance of String call a protected method of String?  I don't think the
> enclosing "Foo" class definition is relevant here, is it?

You are confusing caller and receiver. The caller is the "enclosing"
object and receiver is the object whose method is being called:
think of caller being analogous to scope and receiver being analogous to an
lvalue.

class Caller < SuperKlass
  def calling_method
     receiver.foobar  # <-- receiver is the object left of the dot.
  end                 #     and caller is an instance of Caller.
end

Now, if receiver is an instance of SuperKlass or Caller, the method
"foobar" may be protected.

Any help?

 -- Nikodemus



Mon, 08 Nov 2004 16:25:15 GMT  
 confused about access control

Quote:
> Stop saying "call" and instead say "send a message" because that's
> where the confusion arises.

/k dossy Stop telling me what to do!  (For everyone else, inside joke; Dossy and
I go back a ways.)

I don't have a Smalltalk background, so the "sending a message" paradigm is
indeed where the confusion arose, but not because it's more clear to me.

Anyway, I think I've unraveled it in my pointy little brain; thanks.  <excuse
type="lame" value="It's early, I don't have my glasses on yet, and haven't had
my first coffee."  />



Mon, 08 Nov 2004 21:11:14 GMT  
 confused about access control

Quote:
> > Stop saying "call" and instead say "send a message" because that's
> > where the confusion arises.

> /k dossy Stop telling me what to do!  (For everyone else, inside joke;
> Dossy and I go back a ways.)

/k UG vengence is a {*filter*}, {*filter*}!  ;-)

;)

Quote:
> I don't have a smalltalk background, so the "sending a message" paradigm is
> indeed where the confusion arose, but not because it's more clear to me.

I always said that Java would lead to brain damage!

Indeed, once you think aobut the object oriented paradigm as involving,
guess what, objects ... and that they are merely communicating with each
other by sending each other little love letters (well, messages anyhow)
then lots of things start to make sense.

At least, it did for me ...

-- Dossy

--

Panoptic Computer Network             web: http://www.*-*-*.com/
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)



Mon, 08 Nov 2004 21:21:38 GMT  
 confused about access control

Quote:
> > I don't have a smalltalk background, so the "sending a message"
> paradigm is
> > indeed where the confusion arose, but not because it's more clear
> to me.

> I always said that Java would lead to brain damage!

Worse; C++ then Java.

Quote:
> At least, it did for me ...

I'm working on it man.  You guys need to go slow, I'm old.

=====
--
Yahoo IM: michael_s_campbell

__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com



Mon, 08 Nov 2004 22:29:22 GMT  
 confused about access control
Quote:

> > I always said that Java would lead to brain damage!

> Worse; C++ then Java.

Amen! For those of us who fell in love with Java because it was so much
less cumbersome than C++, Ruby makes us think we've died and gone to
heaven.


Mon, 08 Nov 2004 23:37:02 GMT  
 confused about access control

Quote:

> foo is an instance of String, but Foo isn't a subclass of String.
> Even if Foo were a subclass of String, foo.undot! is trying to publically
> access the undot! method.

Or, to put it another way:

Quote:
>> irb(main):007:0> class Foo
>> irb(main):008:1>   foo = '..123'
>> irb(main):009:1>   foo.undot!
>> irb(main):010:1> end
...
>> "Protected methods can be invoked only by objects of the defining
>> class and its subclasses."

The object that is /invoking/ undot! is Foo, not foo, and Foo has nothing to
do with String.

--
 |..  "To understand recursion, you need to understand recursion."
<|>   -- anon
/|\  
/|    
 |        



Wed, 10 Nov 2004 00:14:27 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Accessing a control from control array

2. confused if i should be doing type checking in the code ( a confused java programmer)

3. confused if i should be doing type checking in the co de ( a confused java programmer)

4. Access to database Users, Groups privileges control functionality from Smalltalk

5. Object3D.Shape - ways to access/control multiple shapes?

6. Access window control from an other procedure

7. Procedure Access Control

8. EIP - Form / EIP access control.

9. Access Control of Applications in a Corporate Environment

10. accessing form controls from another form

11. Controlling Access in Labview

12. Version control and multiuser access on one project

 

 
Powered by phpBB® Forum Software