|
|
||
class MyClass define_method :my_method do |my_arg| my_arg * 3 end end obj = MyClass.new obj.my_method(2) # => 6
動的メソッドと呼ばれる
class Foo private def print(text) p text end end Foo.new.send(:print, 'hoge') # => "hoge" Foo.new.public_send(:print, 'hoge') # => error
呼び出せてしまう。public_sendを用いることでpublicのメソッドのみ読み出せるので通常はこちらをつかったほうがよさそう。ただ、privateなメソッドのテストを行うときにはsend使うと便利かも
アプリケーションの設定情報をYAMLで保存している
admin : Bill title : Rubyland topic : Ruby and more
これらの設定をconfオブジェクトに格納している。普通に書いたら下記のようになる。
conf.admin = 'Bill' conf.title = 'Rubyland' conf.topic = 'Ruby and more'
ただ、Campingのコードには下記のようなソースはない。
YAML.load_file(conf.rc).each do |k, v| conf.send("#{k}=", v) end
class MyClass def my_method(my_arg) my_arg * 2 end end obj = MyClass.new p obj.my_method(3) p obj.send(:my_method, 3)
後者のsendを使えばメソッドを動的に呼び出せる。この技術を動的ディスパッチと呼ぶ。
module Printable def print; end end module Document def print; end end class Book include Document end class Book2 include Document include Printable end Book.ancestors # => [Book, Document, Object, Kernel, BasicObject] Book2.ancestors # => [Book2, Printable, Document, Object, Kernel, BasicObject]
include Documentした際にはBook2の上にDocumentの継承チェーンを作る。include Printableの時にBook2の上にPrintableが配置され、Documentはひとつ上に押し上げられる。なので、例えばBook2でprintメソッドを呼び出した場合はPrintableモジュールのprintが実行される。
testing_selfを呼び出すとobjがselfになる。@varはobjのインスタンス変数になる。
class MyClass def testing_self @var = 10 my_method() self end def my_method @var = @var + 1 end end obj = MyClass.new p obj.testing_self # => #<MyClass:0x0000010092e180 @var=11>
常にselfのオブジェクトが何かを意識すること。
methodを呼び出さないとき、selfはどうなっているのか。
ruby-head :011 > self => main ruby-head :012 > self.class => Object
ruby-head :013 > class X; self; end => X
モジュールやクラスの定義のなかでは、selfはモジュールやクラスになる。
class C def public_method self.private_method end private def private_method; end end C.new.public_method
これはもちろん実行できない。privateキーワードは明示的なレシーバーをつけてプライベートメソッドを呼び出すことはできない。暗黙的なレシーバーselfに対するものでなければならない。よって、上記のコードはselfを削除すれば動く
print()みたいなどこからでも呼び出せるメソッドはKernelモジュールのプライベートインスタンスメソッド。
ObjectクラスがKernelモジュールをincludeしている
ruby-head :010 > Kernel.private_instance_methods.grep(/^pr/) => [:printf, :print, :proc]
このメソッドで継承チェーンを知ることが可能となる
class MyClass def my_method 'my_method()' end end class MySubclass < MyClass end obj = MySubclass.new p obj.my_method() p MySubclass.ancestors # => [MySubclass, MyClass, Object, Kernel, BasicObject]
my_method()を読みだすとRubyはレシーバであるobjからMySubcclassを検索。MySbuclassクラスにもメソッドがないので、さらに上位クラスのMyclassを探す。
class MyClass def my_method 'my_method()' end end class MySubclass < MyClass end obj = MySubclass.new p obj.my_method()
module Rake class Task …
Taskクラスの完全な名前はRake::Taskになった。これで名前が衝突することがなくなる。Rakeのような定数をまとめただけのmoduleをネームスペースと呼ぶ
現在のスコープにある全ての定数を返す
module M Y = '他の定数' class C ::M::Y end end p M.constants # => [:Y, :C]
クラス名Cも定数。
Module.constants()は現在のプログラムのトップレベルにある定数を返す。
ruby-head :008 > Module.constants => [:Object, :Module, :Class, :Kernel, :NilClass, :NIL, :Data, :TrueClass, :TRUE, :FalseClass, :FALSE, :Encoding, :Comparable, :Enumerable, :String, :Symbol, :Exception, :SystemExit, :SignalException, :Interrupt, :StandardError, :TypeError, :ArgumentError, :IndexError, :KeyError, :RangeError, :ScriptError, :SyntaxError, :LoadError, :NotImplementedError, :NameError, :NoMethodError, :RuntimeError, :SecurityError, :NoMemoryError, :EncodingError, :SystemCallError, :Errno, :ZeroDivisionError, :FloatDomainError, :Numeric, :Integer, :Fixnum, :Float, :Bignum, :Array, :Hash, :ENV, :Struct, :RegexpError, :Regexp, :MatchData, :Marshal, :Range, :IOError, :EOFError, :IO, :STDIN, :STDOUT, :STDERR, :ARGF, :FileTest, :File, :Dir, :Time, :Random, :Signal, :Process, :Proc, :LocalJumpError, :SystemStackError, :Method, :UnboundMethod, :Binding, :Math, :GC, :ObjectSpace, :Enumerator, :StopIteration, :RubyVM, :Thread, :TOPLEVEL_BINDING, :ThreadGroup, :Mutex, :ThreadError, :Fiber, :FiberError, :Rational, :Complex, :RUBY_VERSION, :RUBY_RELEASE_DATE, :RUBY_PLATFORM, :RUBY_PATCHLEVEL, :RUBY_REVISION, :RUBY_DESCRIPTION, :RUBY_COPYRIGHT, :RUBY_ENGINE, :ARGV, :Gem, :TSort, :RbConfig, :Config, :CROSS_COMPILING, :Exception2MessageMapper, :IRB, :RubyToken, :RubyLex, :Readline, :Date, :BasicObject]
現在のパスが必要ならばModule.nesting()を使う
module M class C module M2 p Module.nesting # => [M::C::M2, M::C, M] end end end
::で書き始めて外部の定数を絶対パスで指定する。
module M Y = '他の定数' class C ::M::Y end end ||<< * [meta] classのクラス >|ruby| ruby-head :003 > String.class => Class ruby-head :004 > Class.class => Class ruby-head :005 > Class.superclass => Module ruby-head :006 > Module.superclass => Object ruby-head :007 > String.superclass => Object