class Person
attr :sex
attr_reader :name
attr_writer :name
attr_writer :sex
attr_accessor :facebook
def greeting
"Hello #{@facebook}"
end
private
attr :occupation
attr_accessor :occupation
end
module Klass
def greeting
"Hello from Klass.\n"
end
end
person = Person.new
person.name = "Dennis"
person.sex = "male"
person.instance_eval do
def fool_i
puts ("Hello instance_eval foo_i")
end
end
puts(person.singleton_methods)
puts("person对象的复制:")
puts(person.dup.instance_variables)
puts("person对象继承:")
puts(person.extend(Klass).greeting)
puts("person对象与模块Klass对象类相等么?")
puts(person.eql?Klass)
puts("person对象与自身类person相等么?")
puts(person.eql?person)
fool_i
person对象的复制:
@name
@sex
person对象继承:
Hello from Klass.
person对象与模块Klass对象类相等么?
false
person对象与自身类person相等么?
true
开发工具:RubyMine 2022.2.1
参考文献:https://ruby-china.org/topics/25739