In Ruby, private methods in super class can be called from sub class. If you are a Java or C# programmer, you would be surprised at such behavior like me.
class Foo protected def protected_method_in_foo "Hello." end private def private_method_in_foo "Bye." end end class Bar < Foo def say_hello protected_method_in_foo end def say_bye private_method_in_foo # OK end end bar = Bar.new puts bar.say_hello # Hello. puts bar.say_bye # Bye.
The difference between private methods and protected methods is a little difficult too. Protected methods can be called from the instances of sub classes via receivers, but private methods cannot do the same.
class Foo protected def protected_method_in_foo "Hello." end private def private_method_in_foo "Bye." end end class Bar < Foo def say_hello_via(foo) foo.protected_method_in_foo end def say_bye_via(foo) foo.private_method_in_foo # NG!! end end foo = Foo.new bar = Bar.new puts bar.say_hello_via foo # Hello. #puts bar.say_bye_via foo # NoMethodError!!
Matz answers why
I wanted to know why Ruby is designed like this, so I asked Yukihiro "Matz" Matsumoto. He readily answered my questions. I am thankful to him.
@yukihiro_matz さん、こんなブログ( d.hatena.ne.jp/JunichiIto/201… )を書いたんですが、もう少し掘り下げたいので質問させて下さい。privateメソッドをサブクラスから呼べるようにした設計思想を知りたいです。またこういう仕様を持つ言語は他にもありますか?
— Junichi Ito (伊藤淳一) (@JunichiIto77) March 14, 2012
Me: I would like to know why Ruby lets sub classes to call private methods in super class. What is the design concept? And are there any programming languages that behave like Ruby?
@JunichiIto77 privateという名前がJavaやC++に馴染んだ人に誤解を生みやすいのは想定外でした。Rubyを最初に設計した頃にはJavaは一般公開されてませんでしたし、private(など)はC++固有の機能でしたから尊重しませんでした
— Yukihiro Matsumoto (@yukihiro_matz) March 15, 2012
Matz: I did not expect that Java or C++ programmers tend to misunderstand the keyword "private". When I designed Ruby for the first time, Java had not been released. And I did not consider "private" (and other keywords) because they are C++ specific functions.
@JunichiIto77 Rubyのprivateの発想の元になったのはSmalltalkの「privateカテゴリ」です。使わないでね、というだけでアクセスできちゃう。Rubyはそれよりは若干強制力があります。Rubyの反C++・親Smalltalkの設計思想が垣間見えますね
— Yukihiro Matsumoto (@yukihiro_matz) March 15, 2012
Matz: Ruby's "private" was inspired by the "private categories" in Smalltalk, which means just "Please do not use", but they are accessible. Ruby is a little stricter than Smalltalk. That is a part of the concept - anti-C++ and pro-Smalltalk.
@JunichiIto77 後でprotectedを追加したのもまずかった。これでC++とキーワードが同じでも意味がズレてることになってしまったので。
— Yukihiro Matsumoto (@yukihiro_matz) March 15, 2012
Matz: The keyword "protected" was introduced later, but that was not a good idea because that had brought the same keyword and the different meaning between Ruby and C++.
今だったらどんなキーワードにするのか気になるところ。 QT @yukihiro_matz bit.ly/yVo3RA
— あおい (@aoi0308) March 15, 2012
Other person: If Matz introduced the function now, what keyword would he choose? I wonder.
@aoi0308 今だったら? protected入れない。
— Yukihiro Matsumoto (@yukihiro_matz) March 15, 2012
Matz: If now? I would not introduce "protected".
Summary
The Ruby was designed prior to Java/C# and the keyword "private" was inspired by Smalltalk. That is why the behavior between Ruby and Java/C# is different.
I wrote the same article in Japanese: http://d.hatena.ne.jp/JunichiIto/20120315/1331754912
No comments:
Post a Comment