{title: :MetaprogrammingRuby, reading: '1st', week: :Tuesday}
2.2
動的メソッドって余り使ったこと無いっす><
2.2.2
動的メソッドな定義も使ったことないw;
ruby-1.9.2-p136 :001 > class MyClass ruby-1.9.2-p136 :002?> def my_method arg ruby-1.9.2-p136 :003?> arg*2 ruby-1.9.2-p136 :004?> end ruby-1.9.2-p136 :005?> end => nil ruby-1.9.2-p136 :006 > obj = MyClass.new => #<MyClass:0x897d210> ruby-1.9.2-p136 :007 > obj.my_method 3 => 6 ruby-1.9.2-p136 :008 > obj.send :my_method,9 => 18 ruby-1.9.2-p136 :009 > class MyClass ruby-1.9.2-p136 :010?> define_method :you_method do |arg| ruby-1.9.2-p136 :011 > arg*3 ruby-1.9.2-p136 :012?> end ruby-1.9.2-p136 :013?> end => #<Proc:0x8998100@(irb):10 (lambda)> ruby-1.9.2-p136 :014 > obj.you_method 3 => 9 ruby-1.9.2-p136 :015 > obj.send :you_method,9 => 27
Ruby1.9ではpublic_sendが追加とのこと。
ruby-1.9.2-p136 :026 > class MyClass ruby-1.9.2-p136 :027?> def pub_method;end ruby-1.9.2-p136 :028?> def pri_method;end ruby-1.9.2-p136 :029?> private :pri_method ruby-1.9.2-p136 :030?> end => MyClass ruby-1.9.2-p136 :031 > obj=MyClass.new => #<MyClass:0x898da20> ruby-1.9.2-p136 :032 > obj.send :pub_method => nil ruby-1.9.2-p136 :033 > obj.send :pri_method => nil ruby-1.9.2-p136 :034 > obj.public_send :pub_method => nil ruby-1.9.2-p136 :035 > obj.public_send :pri_method NoMethodError: private method `pri_method' called for #<MyClass:0x898da20> from (irb):35:in `public_send' from (irb):35 from /home/murase/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in `<main>'
2.3
method_missingキターーー
2.5.1
BasicObjectは継承できないのか?
説明読んで、ブランクスレート作成用かと思ったけど
ruby-1.9.2-p136 :039 > BasicObject.instance_methods => [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__] ruby-1.9.2-p136 :040 > class MyClass < BasicObject;end TypeError: superclass mismatch for class MyClass from (irb):40 from /home/murase/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in `<main>'
メタプログラミングRubyposted with amazlet at 11.03.04