Skip to content

Instantly share code, notes, and snippets.

@mrorii
Created March 27, 2010 11:36
Show Gist options
  • Select an option

  • Save mrorii/345966 to your computer and use it in GitHub Desktop.

Select an option

Save mrorii/345966 to your computer and use it in GitHub Desktop.
class Hoge
def initialize(state, foo)
@state = state
@foo = foo
end
# セッター
def state=(new_state)
@state = new_state
end
# ゲッター
def state
@state
end
def print
puts "@state is #{@state} and @foo is #{@foo}"
end
end
h = Hoge.new("aho", 1)
h.print #=> @state is aho and @foo is 1
h.state = "baka" # 8 行目の state= を呼ぶ
h.print #=> @state is baka and @foo is 1
h.foo = "yukarin" #=> def foo=(...) を定義していないのでエラー
h.foo #=> def foo を定義していないのでエラー
puts h.state #=> baka
# 12 行目で def state というゲッター(getter)を定義したので、
# @state の値を取得できる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment