ミームの死骸を待ちながら

We are built as gene machines and cultured as meme machines, but we have the power to turn against our creators. We, alone on earth, can rebel against the tyranny of the selfish replicators. - Richard Dawkins "Selfish Gene"

We are built as gene machines and cultured as meme machines, but we have the power to turn against our creators.
We, alone on earth, can rebel against the tyranny of the selfish replicators.
- Richard Dawkins "Selfish Gene"

今日思い出したことのメモ -インスタンス変数-


必要なプログラムを好きなように作りながら思う。プログラミングの練習プロセスにおいて、「クラスの利用」が一つの壁なんじゃなかろうか。

メモ

hoge = 2

def double(num)
  puts num*hoge
end
  
>double(100)
=> in `double': undefined local variable or method `hoge' for #<Object:0x2a955cb420> (NameError)

で、↓だと成功。

@hoge = 2

def double(num)
  puts num*@hoge
end

>double(100)
=> 200


あと、インスタンス変数をclass Hogeのdef initializeで囲ってやると、そのクラスのインスタンスHoge.newを作成したときにインスタンス変数が読まれる*1
プログラム楽しい。うちの研究室では、広い視野を持たせるため学部と修士でテーマをがらりと変えるのが慣例なのだが、今年いい成果出して修士のテーマもバイオインフォマティクス系をやりたいと主張してみよう。

*1:『たのしいRuby第二版』p.122