Created
May 6, 2010 01:39
-
-
Save notahat/391685 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A | |
| def hello | |
| "A#hello" | |
| end | |
| def method_missing(method, *args) | |
| "A#method_missing" | |
| end | |
| protected :hello | |
| end | |
| p A.new.hello # => "A#method_missing" |
Author
Ok, clearly I didn't get the point across. Let me give another example:
class A
def hello
"A#hello"
end
end
class B < A
def hello
"B#hello"
end
protected :hello
end
p A.new.hello # => "A#hello"
I expected that, if I declare a method as protected, I would always get an exception when I try to call the method from outside the class. Instead, I get the superclass method if one is defined.
It's not protected in class A.
p B.new.hello gives me a
NoMethodError: protected method `hello' called for #<B:0x1014b2d08>
from (irb):16
Author
Crap, stuffed that up. Ok, try this on for size:
class A
def hello
"A#hello"
end
def method_missing(method, *args)
"A#method_missing"
end
end
class B < A
def hello
"B#hello"
end
def goodbye
"B#goodbye"
end
protected :hello, :goodbye
end
p B.new.hello # => "A#method_missing"
p B.new.goodbye # => "A#method_missing"
And also
A.new.hello
=> "A#hello"
Right, so you can hide functionality in a subclass that is provided by the superclass.
That sounds like a bad idea, since it sort of stops you being able to rely on object-oriented polymorphism.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah!
I've become so spoilt by ruby. Nice syntax like this just doesn't get the appreciation it deserves.