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(‘aaa’, 11, ‘男’, ‘13900000001’)
u.name
=> “aaa”
u.phone
NoMethodError: undefined method `phone’ for #<U:0x00007ff19c9d7458>
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’]
=> “13200000001”
u.instance_variable_set(:@phone, ‘13200000002’)
=> “13200000002”
u.instance_variable_get(:@phone)
=> “13200000002”