変数とselfとか newについて

http://cyakarin.kuronowish.com/index.cgi?create_obj
こちらを参考に

newは以下の2つの処理をおこなう

  • オブジェクトの生成(allocate)
  • オブジェクトの初期化(initialize)
[hirasawa@centos-hira ruby_kuma]$ cat new.rb
#!/usr/local/bin/ruby
class Country
p self
p self.class
p self.class.ancestors
end

country = Country.new
p country.class
p self
p self.class
p self.class.ancestors

p Country.method("method")

p Country.method("new")


[hirasawa@centos-hira ruby_kuma]$ ruby new.rb
Country
Class
[Class, Module, Object, Kernel, BasicObject]
Country
main
Object
[Object, Kernel, BasicObject]
#<Method: Class(Kernel)#method>
#<Method: Class#new>
[hirasawa@centos-hira ruby_kuma]$

[hirasawa@aspire-white ruby]$ cat check.rb 
class Hoge 
 @vari1="A"
 p "self"
 p self
 p "self.ancestors"
 p self.ancestors
 
 def initialize(a=self,b=Time.now)
 p "in initialize"
  @vari1="B"
  @vari2="C"
 p "self"
 p self
  p a
  p b
 end

 def check
 p "I am just check"
 p "self"
 p self
 p  @vari1
 p  @vari2
  end

 def self.check
 p "I am self.check"
 p "self"
 p self
 p  @vari1
 p  @vari2
 end
end

 p "#{Time.now}"
 sleep 1
 hoge = Hoge.new
 hoge.check
 Hoge.check
 p "hoge.class.ancestors"
 p hoge.class.ancestors
 p "Hoge.class.ancestors"
 p Hoge.class.ancestors
[hirasawa@aspire-white ruby]$ 
[hirasawa@aspire-white ruby]$ 
[hirasawa@aspire-white ruby]$ ruby check.rb 
"self"       >> in Main
Hoge         self is Hoge
"self.ancestors"
[Hoge, Object, Kernel]  >> Hogeの祖先
"Sun Jan 20 09:20:43 +0900 2013"  >> ここからプログラム開始
"in initialize"          >> Hoge.newによってinitが起動
"self"
#<Hoge:0xb776e8c0 @vari1="B", @vari2="C"> >> init() の中の p self
#<Hoge:0xb776e8c0 @vari1="B", @vari2="C">  >> init()の中の p a
Sun Jan 20 09:20:44 +0900 2013             >> init()の中の p b (一秒後ですね)
"I am just check"
"self"
#<Hoge:0xb776e8c0 @vari1="B", @vari2="C"> >> hoge.cehck()の中の p self
"B"   >> hoge.check()の中の p @vari1
"C"   >> hoge.check()の中の p @vari2
"I am self.check" >> Hoge.check()から  self.check()が呼ばれる
"self"
Hoge  >> self.check() の中の p self
"A"     >> @vari1
nil     >> @vari2
"hoge.class.ancestors"
[Hoge, Object, Kernel]
"Hoge.class.ancestors"
[Class, Module, Object, Kernel]
[hirasawa@aspire-white ruby]$