|
|
||
特定のオブジェクトにメソッドを追加できる。
str = 'hello world' def str.title? self.upcase == self end p str.title? p str.methods.grep(/hello/) p str.singleton_methods
class MyClass < Array def my_method 'Hello' end end
これと同じ動きをするものを、classキーワードを用いずに書く。
class Class.new(Array) def my_method 'Hello' end end
class Loan def initialize(book) @book = book @time = Time.now end def to_s "#{@book.upcase} loaded on #{@time}" end end
次の用にコードを帰る
class Loan def initialize(book) @book = book @time = Loan.time_class.now end def self.time_class @time_class || Time end def to_s "#{@book.upcase} loaded on #{@time}" end end
実際に使う際には、@time_classは常にnilになるので、Timeが呼ばれる。テストコードでは、@time_classにダミーの時間を挿入してテストすると良い。以下、テストコード
require 'test/unit' class FakeTime def self.now '2011-06-21 21:14:44 +0900' end end class Test_Loan < Test::Unit::TestCase def test_conversion_to_string Loan.instance_eval { @time_class = FakeTime } loan = Loan.new('War and Peace') assert_equal "WAR AND PEACE loaded on #{FakeTime.now}", loan.to_s end end
Module#class_evalを使用する
def add_method(a_class) a_class.class_eval do def m 'Hello' end end end add_method String 'a'.m # => 'Hello'