提交 b145a725 编写于 作者: J José Valim

Merge pull request #1297 from cesario/1294-allocated-object-cant-be-inspected

#1294 : allocated object cant be inspected [closes #1294]
......@@ -1870,12 +1870,16 @@ def readonly!
# Returns the contents of the record as a nicely formatted string.
def inspect
attributes_as_nice_string = self.class.column_names.collect { |name|
if has_attribute?(name)
"#{name}: #{attribute_for_inspect(name)}"
end
}.compact.join(", ")
"#<#{self.class} #{attributes_as_nice_string}>"
inspection = if @attributes
self.class.column_names.collect { |name|
if has_attribute?(name)
"#{name}: #{attribute_for_inspect(name)}"
end
}.compact.join(", ")
else
"not initialized"
end
"#<#{self.class} #{inspection}>"
end
protected
......
......@@ -109,6 +109,14 @@ def test_respond_to_with_allocated_object
assert_respond_to topic, :title
end
# IRB inspects the return value of "MyModel.allocate"
# by inspecting it.
def test_allocated_object_can_be_inspected
topic = Topic.allocate
assert_nothing_raised { topic.inspect }
assert topic.inspect, "#<Topic not initialized>"
end
def test_array_content
topic = Topic.new
topic.content = %w( one two three )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册