我目前正在研究about_classes.rb。我对检查的概念以及它与self的关系感到困惑。调用对象会自动返回该对象的检查方法吗?
class Dog7
attr_reader :name
def initialize(initial_name)
@name = initial_name
end
def get_self
self
end
def to_s
__
end
def inspect
"<Dog named '#{name}'>"
end
end
def test_inside_a_method_self_refers_to_the_containing_object
fido = Dog7.new("Fido")
fidos_self = fido.get_self
assert_equal __, fidos_self
end
def test_to_s_provides_a_string_version_of_the_object
fido = Dog7.new("Fido")
assert_equal __, fido.to_s
end
def test_to_s_is_used_in_string_interpolation
fido = Dog7.new("Fido")
assert_equal __, "My dog is #{fido}"
end
def test_inspect_provides_a_more_complete_string_version
fido = Dog7.new("Fido")
assert_equal __, fido.inspect
end
def test_all_objects_support_to_s_and_inspect
array = [1,2,3]
assert_equal __, array.to_s
assert_equal __, array.inspect
assert_equal __, "STRING".to_s
assert_equal __, "STRING".inspect
end