ã¢ã¸ã¥ã¼ã«ã®ç¹ç°ã¡ã½ãããincludeå ã§åå®ç¾©ãã
http://www.jitu.org/~tko/doc-jp/java-ruby/java-ruby-lang.html
ã¢ã¸ã¥ã¼ã«ã§å®ç¾© ãããç¹ç°ã¡ã½ãã(ã¯ã©ã¹ã«ãããã¯ã©ã¹ã»ã¡ã½ããã«ç¸å½ãããã®)ã¯åå®ç¾©ã§ãã¾ããã
eigenclassã¨module_evalã¨define_methodã使ãã°ã§ããªããã¨ã¯ãªãâ¦ã¨ã¤ã£ããã§ããã
ãã ãRuby 1.8ã ã¨ãããã¯ä»ãã®ã¢ã¸ã¥ã¼ã«ã®ç¹ç°ã¡ã½ããã¯ç¡çãããdefine_methodã®ãããã¯å¼æ°ã«&å¼æ°ã渡ããªãããã
RUBY_VERSION # => "1.9.0" module MyMod def self.mod_method true end def self.with_block yield*2 end def self.included(klass) (class << klass; self; end).module_eval do MyMod.methods(false).each do |meth| next if meth.to_s == 'included' define_method(meth){|*args,&block| MyMod.__send__(meth, *args, &block)} end end end end class MyClass1 include MyMod end class MyClass2 include MyMod def self.mod_method # !> method redefined; discarding old mod_method false end end MyClass1.mod_method # => true MyClass2.mod_method # => false MyMod.with_block{2} # => 4 MyClass1.with_block{2} # => 4
追è¨
def self.ã¡ã½ããå end
ã¯ãç¹ç°ã¡ã½ãããã¨ãããã ãã©ãã¾ã é£ããã¨æãããã¯ã©ã¹ãã¢ã¸ã¥ã¼ã«ãã¡ã½ããã«ã¤ãã¦ãã¡ãã¨ç解ããã®ãå
ãç¹ç°ã¡ã½ããâ¦ãã ãã¦è¨ãã¨ãªãã¸ã§ã¯ãã«åæ§ãã¤ãããã®ã ã¨ã¤ã¡ã¼ã¸ããã°ãããæ ã«ã¨ããããªã人éãããããã«ãRubyã®ãªãã¸ã§ã¯ãã§ãæ ã«ã¨ããããªããªãã¸ã§ã¯ããããã¨ãã話ã
ãtrueãã¯å®è¡ã¨ãããå¼ãRubyã¯æå¾ã«è©ä¾¡ï¼å®è¡ï¼ããå¼ãè¿ãå¤ã«ãªããããã®å ´åã¯è¿ãå¤ã«ãªãã
追è¨
モジュールメソッドをクラスに継承させる, primeについて その2, MathMLライブラリ0.8.1を公開しました, お詫び - 忘れたときに備えた記録(2008-03-23)
ãã©ãã¯ããã¯ãããã¨ããå®ã¯ä¸ã®ã³ã¼ãã¯åæãå
ã«ãããã¤ãªã®ã§ç
©é(^^;
ã ã£ãããã£ãã®ãã¨extend selfããããï¼
module Mod extend self def hoge "HOGE" end def self.included(klass) klass.extend(self) end end class C include Mod end Mod.hoge # => "HOGE" C.hoge # => "HOGE"