Ruby ã®è½ã¨ãç©´ãï¼
id:sumim:20070102:p1 ã®ç¶ãã®ãããªãããã§ãªããããªâ¦ã
Ruby ã®ãããããâã¤ã³ã¹ã¿ã³ã¹ãã¼ã¹âï¼èªå¼ãããã®ã§ä½¿ãã®ãé¿ããããã«ãã¦ãã¾ãããä¸è¬ã«ã¯ããããã¿ã¤ããã¼ã¹ãï¼çãªå´é¢ã¨ãã¦ããç´¹ä»ããããã®ã«ããç¹ç°ã¡ã½ãããã¨å¼ã°ããã¤ã³ã¹ã¿ã³ã¹ç¹ç°çã¡ã½ãããããã¾ãããããããã¤ã®ã¨ãã Ruby ã®ããã¯ãç¹ç°ã¯ã©ã¹ãã¨ããç¹æ®ãªä½ç½®ã¥ãã®å¿åã¯ã©ã¹ã«ãããããã£ã¦âã¯ã©ã¹ãã¼ã¹âçãªèãæ¹ã«ããå®ç¾ããã¦ãããã®ã§ãã£ã¦ãå°ãªãã¨ãä»çµã¿ã¨ãã¦ã¯ãã¤ã³ã¹ã¿ã³ã¹ãã¼ã¹ããã¯ã»ã©é ãã¨ããã«ããæ©è½ã â¦ã¨ããã®ã¯ãã¤ã³ã¹ã¿ã³ã¹ãã¼ã¹ã®èãæ¹ãç解ãã人åãã«ã¯ããã種ã®ãè½ã¨ãç©´ãã ã¨è¨ãããã§ãã
åå¾ãã¾ãããã¯ã©ã¹ãã¼ã¹ã«ã¯ãªãã¤ã³ã¹ã¿ã³ã¹ãã¼ã¹ã®æ¬è³ªã¯ï¼ããè¨ãããããã«ãnewã使ããcloneã使ãã¨ããã¯ã©ã¹ã®æãã»ç¡ãããããã¯ãç¶æ¿ã§ã¯ãªãå§è²ãã¼ã¹ã ã¨ãã§ã¯ãªãâ¦ï¼ãã¤ã³ã¹ã¿ã³ã¹ãã¯ã©ã¹ãä»ããã«åå¥ã®å¤æ°ãã¡ã½ãããæã¦ããã©ãããã«å°½ãã¾ããããããåãå£ã§ã¯ãRuby ã«ããã¦çã«ã¤ã³ã¹ã¿ã³ã¹ãã¼ã¹çãªãã®ãæããã¨ããã°ãã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®æ¯ãèãã®ã»ãããã£ã¨ãµããããã§ãããã
Smalltalk ã Java ãªã©ã®ã¯ã©ã¹ãã¼ã¹ã®è¨èªã«ãããé常ã¨ã¯ç°ãªããRuby ã®ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã¯ä»£å ¥å¼ããã®å®£è¨ãå ¼ããããã§ããã¤ã¾ãããã§ã«åå¨ããã°ããã使ããããªããã°æ°ãã«ä½ãâ¦ã¨ããæ¯ãèãããã¾ãããã®ã¨ãããã¹ã¦ã®ã¤ã³ã¹ã¿ã³ã¹ï¼å¿ è¦ãªããã¹ã¦ã®ãµãã¯ã©ã¹ã«å±ããã¤ã³ã¹ã¿ã³ã¹ï¼ã«è¿½å ãããããã§ã¯ãªããããã¾ã§æ³¨ç®ãã¦ããã¤ã³ã¹ã¿ã³ã¹ã®ã¿ã«éãããâ¦ã¨ããç¹ã§âã¤ã³ã¹ã¿ã³ã¹ãã¼ã¹âçã§ããããã§ãã
ãã¨ãã° Ruby ã®å ´åãã¯ã©ã¹ Foo ã«ããã¦ãã¤ã³ã¹ã¿ã³ã¹ã @var ã¨ããã¤ã³ã¹ã¿ã³ã¹å¤æ°ãæã¤ãã¨ãæå¾ ãã¦ãããã«ã¢ã¯ã»ã¹ããããã®ã¡ã½ãããå®ç¾©ããã¨ãã¦ããçæããã¦ã® Foo ã®ã¤ã³ã¹ã¿ã³ã¹ã¯ã¾ã @var ã¨ããâã¹ãããâãæã£ã¦ãã¾ããããã ãã®ã¢ã¯ã»ããµãä»ã㦠@var ã«ã¢ã¯ã»ã¹ï¼å³å¯ã«ã¯åç §ããã ãã§ã¯ãã¡ã§ãä»£å ¥ï¼ããã¢ã¯ã·ã§ã³ããã£ã¦ã¯ãã㦠@var ã¨ããã¹ãããã確ä¿ããã¾ãã
class Foo; attr_accessor :var end foo1 = Foo.new foo2 = Foo.new p [foo1, foo2].collect { |foo| foo.instance_variables } #=> [[], []] foo1.var p [foo1, foo2].collect { |foo| foo.instance_variables } #=> [[], []] foo1.var = :something p [foo1, foo2].collect { |foo| foo.instance_variables } #=> [["@var"], []]
ããã§ãç¥ãããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ã¤ã㦠*ã¯ã©ã¹ã«* ã§ã¯ãªãã*ã¤ã³ã¹ã¿ã³ã¹ã«* å°ãã¦ããã¨ããããã½ã§ããJava ã Smalltalk ãªã©ã§ã¯ãã¤ã³ã¹ã¿ã³ã¹ãã¯ã©ã¹ã§å®£è¨ãããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®ã¹ããããæã¤ãã¨ã¯èªæãªã®ã§ãããããâåãåããâã¯ãµã¤ãã¯ã©ã¹ã«å¯¾ãã¦è¡ãªããã¾ãã
Java ã®å ´å
import java.lang.reflect.Field; import java.util.ArrayList; class Foo { public int var; } public class Main { public static void main(String[] args) { Field[] fields = Foo.class.getDeclaredFields(); ArrayList<String> instVarNames = new ArrayList<String>(); for (Field field : fields) { instVarNames.add(field.getName()); } System.out.println( instVarNames ); } } //=> [var]
Smalltalk ã®å ´å
Object subclass: #Foo instanceVariableNames: 'var' classVariableNames: '' poolDictionaries: '' category: 'Category-Name'
Foo instVarNames "=> #('var') "
ãã¤ã®ã¨ãããRuby ã®ãªãã¸ã§ã¯ãã«ã¤ã³ã¹ã¿ã³ã¹å¤æ°ç¨ã®ã¹ãããã追å ããã®ã«ã¯ãã¤ã³ã¹ã¿ã³ã¹å¤æ°ã¸ã®ã¢ã¯ã»ããµï¼ããªãã¡ *ã¯ã©ã¹ã«* å®ç¾©ãããã¡ã½ããï¼ãä»ããå¿
è¦ãããªãã£ããããã¾ãã
obj1 = Object.new obj2 = Object.new p [obj1, obj2].collect { |foo| foo.instance_variables } #=> [[], []] obj1.instance_eval { @var = :something } p [obj1, obj2].collect { |foo| foo.instance_variables } #=> [["@var"], []]
ã¤ã¾ããRuby ã«ããã¦åã
ã®ã¤ã³ã¹ã¿ã³ã¹ãã©ããªã¹ããããæã£ã¦ããã®ããæ¬æ¥ãªããã®ç®¡ç責任è
ã§ãã£ã¦ãããã¹ãã¯ã©ã¹ã¯ãå®å
¨ã«âè帳ã®å¤âã«ç½®ããã¦ããããã§ãããã®ãã㪠Ruby ã«ãããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®ã¤ã³ã¹ã¿ã³ã¹ãã¼ã¹çå´é¢ã¯ããªãã¬ã¯ã·ã§ã³æ©è½ã«èå³ãæã¡ãã¦ã®äººã«ã¯ãããã¾ããè½ã¨ãç©´ãã«ãªãã®ã§ã¯ãªãããªâ¦ã¨æãã¾ãããä¼¼ããããªãã¨ã¯ãPython ã«ãè¨ãããã§ãã
class Foo: pass foo1 = Foo() foo2 = Foo() print [foo.__dict__.keys() for foo in [foo1, foo2]] #=> [[], []] foo1.var = "something" print [foo.__dict__.keys() for foo in [foo1, foo2]] #=> [['var'], []]
id:sumim:20070108:p1 ã«ç¶ããããªç¶ãããããªâ¦(^_^;)ã