Last active
December 16, 2015 19:59
-
-
Save tenten0213/5489221 to your computer and use it in GitHub Desktop.
おまじないについて その1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hoge | |
def value=(value) | |
@value = value | |
end | |
def value | |
@value | |
end | |
end | |
hoge = Hoge.new | |
hoge.value= 'x' | |
puts hoge.value #=> x | |
class Fuga | |
attr_reader :value | |
attr_writer :value | |
end | |
fuga = Fuga.new | |
fuga.value = 'y' | |
puts fuga.value #=> y | |
class Foo | |
attr_accessor :value | |
end | |
foo = Foo.new | |
foo.value = 'z' | |
puts foo.value #=> z |
コード中にコメントで説明した方がわかりやすいと思うよ。
一度に複数のことを説明しようとしてかえってわかりにくい気がするので、一個づつにしてみれば?
ありがとうございます!分割して、コード内にもコメントいれてみます!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Class Hoge,Fuga,Fooで書いている処理は同じです。
Hogeでの書き方は冗長なので、FugaやFooの記述を基本的には選択します。
attr_readerはJavaのgetter,attr_writerはsetterでしょうか。
attr_accessorはreader,writerをまとめて提供します。