class U
attr_reader :name
attr_writer :age
attr_accessor :sex
def initialize(name, age, sex, phone)
@name = name
@age = age
@sex = sex
@phone = phone
end
end
u = U.new('yanlp', 11, '男', '13900000001')
u.name
=> "yanlp"
u.phone
NoMethodError: undefined method `phone' for #
u.methods.grep /instance/
=> [:instance_values,
:instance_variable_names,
:pretty_print_instance_variables,
:instance_variables,
:instance_variable_get,
:instance_variable_set,
:instance_variable_defined?,
:remove_instance_variable,
:instance_of?,
:instance_eval,
:instance_exec]
u.instance_values['phone']
=> "13900000001"
u.instance_variable_set(:@phone, '13900000002')
=> "13900000002"
u.instance_variable_get(:@phone)
=> "13900000002"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35