26.
Rubyのクラス
class Person
def initialize name
@name = name
end
end
person = Person.new(’matz’)
person.name
# ~> -:7:in ‘<main>’: undefined method
‘name’ for #<Person:0x007fe8409c8eb8 @nam
27.
設定かプログラムか DSL
class Person
attr_accessor :name
end
person.name # => "matz"
person.name = ’MATZ’
person.name # => "MATZ"
28.
代入もメソッド
class Person
def name
@name
end
def name= name
@name = name
end
end
29.
Object
Module
attr accessor
Class Person:Class
30.
メソッドを作るメソッド
def self.my_attr_accessor var
instance_eval do
define_method(var) do
instance_variable_get("@#{var}")
end
define_method("#{var}=") do |val|
instance_variable_set("@#{var}", val)
end
end
end