Rubyã®åããªãã³ã¼ããï¼åç´ç·¨ï¼ãåãã¯ã©ã¹å ãªã®ã«ï¼privateã¡ã½ãããå¼ã¹ãªãå ´åããã
以ä¸ã®Rubyã®ã³ã¼ããï¼æå³ããåä½ãããªãã®ã¯ãªãã§ãããï¼å¶éæéï¼åï¼
ããããäºï¼
- ï¼ã¤ã®ã¯ã©ã¹å ã§å®ç¾©ããã¦ããã¡ã½ãããï¼é çªã«å¼ã³åºãã¦å®è¡ããã
hoge.rb
# ã¯ã©ã¹å®ç¾© class Hoge def self.main_method # ãã®ã¯ã©ã¹ä¸ã§å®ç¾©ããã¦ããã¯ã©ã¹ã¡ã½ããã®å¼ã³åºã some_public_class_method some_private_class_method # ãã®ã¯ã©ã¹ä¸ã§å®ç¾©ããã¦ããã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã®å¼ã³åºã hoge = self.new # ã¤ã³ã¹ã¿ã³ã¹ä½æ hoge.some_public_instance_method hoge.some_private_instance_method end def self.some_public_class_method p "ãããªãã¯ãªã¯ã©ã¹ã¡ã½ããã§ãã" end def some_public_instance_method p "ãããªãã¯ãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã§ãã" end private_class_method def self.some_private_class_method p "ãã©ã¤ãã¼ããªã¯ã©ã¹ã¡ã½ããã§ãã" end private def some_private_instance_method p "ãã©ã¤ãã¼ããªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã§ãã" end end # å ¨ã¦ãå®è¡ Hoge.main_method
çºçããåé¡
publicã¡ã½ããã¯ï¼ã¤ã¨ãæ£ããå®ç¾©ã§ãã¦ããï¼åé¡ã¯èµ·ããªãã
privateãªã¯ã©ã¹ã¡ã½ãããOKã
Rubyã§ã¯ã©ã¹ã¡ã½ãããprivateã«ããæ£ããæ¹æ³
http://blog.s21g.com/articles/561
- privateã§ãã©ã¤ãã¼ããªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãã
- private_class_methodã§ãã©ã¤ãã¼ããªã¯ã©ã¹ã¡ã½ãã
â»ãã ãï¼private_class_methodã§ã¯ãªãç¹ç°ã¯ã©ã¹ã使ãã»ããä¸è¬ç
privateãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã®å¼ã³åºãã§ã¨ã©ã¼ã«ãªãã
ruby -Ks hoge.rb "ãããªãã¯ãªã¯ã©ã¹ã¡ã½ããã§ãã" "ãã©ã¤ãã¼ããªã¯ã©ã¹ã¡ã½ããã§ãã" "ãããªãã¯ãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã§ãã" hoge.rb:14:in `main_method': private method `some_private_instance_method' calle d for #<Hoge:0x287fd78> (NoMethodError) from hoge.rb:47
åå
Rubyã§ã¯ï¼ãprivateãã®æå³ãJavaã¨ç°ãªãã
5.7 ã¡ã½ããã®å¯è¦æ§ãæå®ãããã®ã§ãã
http://www.ruby-lang.org/ja/man/html/...
- Rubyã§ã¯é¢æ°å½¢å¼(ã¬ã·ã¼ããçç¥ããå½¢)ã§ããå¼ã³åºããã¨ã®ã§ããªãã¡ã½ããã®ãã¨ãprivateãªã¡ã½ããã¨å¼ãã§ãã¾ããC++ãJavaã®privateã¨ã¯æå³ãéãã®ã§æ³¨æãã¦ãã ããã
ã¤ã¾ãï¼
some_private_instance_method
ã¨ããã³ã¼ãã¯ï¼ã¡ã½ããã®åã«ã¬ã·ã¼ãããªãã®ã§æ¸ãå¾ããï¼Hogeã®ã¤ã³ã¹ã¿ã³ã¹ã®ä¸ã«éããããï¼ã
ããã
hoge.some_private_instance_method
ã¨ããã³ã¼ãã¯ï¼ã¡ã½ããã®åã«ã¬ã·ã¼ããä»ãã¦ããã®ã§ï¼ãã¨ãHogeã¯ã©ã¹å ã§ãã£ã¦ãæ¸ããªãã®ã ã
Rubyã§ã®äºæ ãã¾ã¨ããã¨ï¼ãããªãã
- ã¯ã©ã¹ã¡ã½ããå ã§ã¯ï¼privateãªã¯ã©ã¹ã¡ã½ãããå¼ã¹ãã
- ã¯ã©ã¹ã¡ã½ããå ã§ã¯ï¼privateãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããå¼ã¹ãªãã
- åä¸ã¯ã©ã¹ã ãï¼åä¸ãªãã¸ã§ã¯ãã§ã¯ãªãã®ã§ã
ãªãï¼Javaã§ã¯äºæ
ãç°ãªãã
public class Hoge { public static void main( String[] args ) { // ãã®ã¯ã©ã¹ä¸ã§å®ç¾©ããã¦ããã¯ã©ã¹ã¡ã½ããã®å¼ã³åºã some_public_class_method(); some_private_class_method(); // ãã®ã¯ã©ã¹ä¸ã§å®ç¾©ããã¦ããã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã®å¼ã³åºã Hoge hoge = new Hoge(); hoge.some_public_instance_method(); hoge.some_private_instance_method(); } public static void some_public_class_method() { System.out.println("public class method"); } public void some_public_instance_method() { System.out.println("public instance method"); } private static void some_private_class_method() { System.out.println("private class method"); } private void some_private_instance_method() { System.out.println("private instance method"); } }
ãã®Javaã®ã³ã¼ãã¯ã¡ããã¨åããï¼ã¯ã©ã¹è¨è¨ã®åãããã¯å¥ã¨ãã¦ï¼
解決çã¨ãã¦ã¯ï¼Rubyã®å ´åã¯ï¼ã¯ã©ã¹è¨è¨ãããç´ãäºã«ãªãã
é¢é£
protectedã«ã¤ãã¦ã触ãã¦ããã
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããå ã§ã¯ï¼åä¸ã¤ã³ã¹ã¿ã³ã¹ã®privateãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããå¼ã¹ãã
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããå ã§ã¯ï¼åä¸ã¯ã©ã¹ã®ä»ã®ã¤ã³ã¹ã¿ã³ã¹ã®privateãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããå¼ã¹ãªãã
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããå ã§ã¯ï¼åä¸ã¯ã©ã¹ã®ä»ã®ã¤ã³ã¹ã¿ã³ã¹ã®protectedãªã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããå¼ã¹ãã