私は現在about_classes.rbに取り組んでいます。inspectの概念とそれがselfにどのように関係しているかについて混乱しています。オブジェクトを呼び出すと、そのオブジェクトのinspectメソッドが自動的に返されますか?
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