Rubyのclass
ざつめも

@hoge
はinstance property
this.
みたいなのは付けない
methodにも、propertyにも
method呼び出し時は ()
も付けない
アクセス修飾子の定義の仕方が独特
rbclass Cat
def meow
puts "Meow!"
end
private
def bowwow
puts "Bowwow!"
end
def cock_a_doodle_doo
puts "cock-a-doodle-doo"
end
end
こう書くと、 bowwow
と cook_a_doodle_doo
がprivateになる
private def bowbow
としても定義できる
論争があるらしい
public/private/protectedは、アクセス制御のために作ったものではないならしい
privateと書いていても外部からアクセスできる
らしい
やり方は知らん
staticみたいなやつだと認識している

def self.hoge
みたいにして、 self.
付けて定義する
こういう感じで使い分けるらしい
attr_readerを付けると
getterの自動実装になる
以下2つは同じ意味
rbclass Wheel
attr_reader :rim
rbclass Wheel
def rim
@rim
end
このコードのdiameterノナカとかで rim
とか tire
みたいに呼び出しているのは、
instance propertyではなく、getter経由のinstance propety
rbclass Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
end
@
を毎回書くよりもgetterを定義しちゃって
@
なして書くほうが楽なので、getterを促す言語デザインになっている

haskellのwhere節のclass版みたいなイメージを抱いた

こんなんあるんや
こういう用途で使うものなのか?
class内のmethodを微妙な距離に隔離するものとして使っている