29th Fri
Rubyのnewとallocate
This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/148
Rubyのnewメソッドとallocateメソッドの挙動に関するメモ。
$ irb
>> class Foo
>> def initialize(a)
>> @foo = a
>> end
>> def foo
>> initialize("test")
>> end
>> end
=> nil
>> a = Foo.new("bb")
=> #<Foo:0x40203ce0 @foo="bb">
>> b = Foo.allocate
=> #<Foo:0x401f4b50>
>> b.foo
=> "test"
>> b
=> #<Foo:0x401f4b50 @foo="test">
new(*args) = allocate + initialize(*args)
ということですね。ファクトリーパターンを実装するのが楽になりそう。
This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/148
posted by
genki on Fri 29 Dec 2006 at 14:40 with 0 comments