[英]Difference between p and puts
Why in below code when I 为什么在我的代码下面
p the object it returns array of integers #=> [[1, 2, 4]] p它返回整数数组的对象#=> [[1,2,4]]
puts the object it returns array of single integers #=>1 2 4 将对象返回单个整数的数组#=> 1 2 4
Class of single integers is still an array, but I do not understand why they return as single numbers. 单个整数类仍然是一个数组,但我不明白为什么它们作为单个数字返回。 Thanks
谢谢
class Matrix
attr_reader :rows, :columns
def initialize(source)
@rows = parse_rows_from_source(source)
@columns = @rows.transpose
end
private
def parse_rows_from_source(source)
source.lines.map { |line| line.split.map(&:to_i)}
end
end
p Matrix.new("1, 2/n3, 4").rows```
p object
is equal to puts object.inspect
p object
等于puts object.inspect
That's the main difference. 这是主要的区别。
Object#inspect
returns a string containing a human-readable representation of object. Object#inspect
返回一个包含人类可读对象表示的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.