2009-05-24から1日間の記事一覧

インスタンス変数やselfの理解整理

あいまいな理解だったので確認してみた。 1 #!/usr/local/bin/ruby 2 class Hoge 3 p self 4 p ancestors 5 @instantce_var1 = "A" 6 def initialize(a=self,b=Time.now) 7 @instantce_var1 = "B" 8 @instantce_var2 = "C" 9 p a 10 p self 11 puts b 12 end…

Refe.vimの操作

~/.vim/ftplugin/ruby.vim に下記二行を追加した前提で、nnoremap K :Refe nnoremap :Refe on ruby source key behave K カーソル下の単語をRefeで引く :Refe Refeでwordを引く Refeを開く on Refe buffer key behave o カーソル下の単語のリファレンスを見…

オブジェクトの凍結(freeze)、clone、dup の複製の深さの違い、eql? と equal? の違い

fc9 [0] /root # irb irb(main):001:0> self.class => Object irb(main):002:0> "str".freeze => "str" irb(main):003:0> a = "str" => "str" irb(main):004:0> a << "b" => "strb" irb(main):005:0> a.freeze => "strb" irb(main):006:0> a << "c" TypeErro…

[Ruby]トップレベルで定義されたメソッドはObjectに属するのでどこからでも呼べる。

#!/usr/local/bin/ruby p self.class def hello(target) puts "hello #{target}" end class Person hello("ryouichi") def hello2 hello("yurichan") end end hello("t9md") Person.new.hello2 実行結果 Object hello ryouichi hello t9md hello yurichan

無名クラスを定数に入れると定数の名前がクラス名になる。

c9 [0] /root # irb irb(main):001:0> var = Class.new => #<Class:0xb8094e40> irb(main):002:0> var.name => "" irb(main):003:0> Wibble = var => Wibble irb(main):004:0> var.name => "Wibble" irb(main):005:0> class Hoge ; end => nil irb(main):006:0> hoge = Hoge.ne</class:0xb8094e40>…