• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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*1 = allocate + initialize*2 ということですね。ファクトリーパターンを実装するのが楽になりそう。

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/148

  1. 1 args

  2. 2 args

posted by Png genki on Fri 29 Dec 2006 at 14:40

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry94
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ