为什么“put”返回空行,而不是“put[]”?

时间:2022-09-10 18:28:10

I'm going through a Ruby tutorial, and learned that the code

我正在学习Ruby教程,并学习了代码

puts 'start'
puts
puts 'end'

will output three lines, but the following code

将输出三行,但下面的代码?

puts 'start'
puts []
puts 'end'

will only output two. The stated reason is that [] isn't an object (edit: "doesn't point to anything"), so puts can't do anything with it, but why is that not also true in the first case?

只会输出两种。声明的原因是[]不是对象(编辑:“不指向任何东西”),所以put不能对它做任何事情,但是为什么在第一种情况下这也不是正确的呢?

I tried to find an official page about puts to figure this out, and this one was no help.

我试着找一个关于put的官方页面来解决这个问题,而这个对我没有帮助。

5 个解决方案

#1


8  

The stated reason is that [] isn't an object

声明的原因是[]不是对象

Stated where?

说明在哪里?

puts has a special handling for arrays. When you pass it an array, it prints each element on a new line. You pass it an array with zero elements, it prints zero lines.

put对数组有特殊的处理。当您将数组传递给它时,它将在新行上打印每个元素。你传递一个零元素的数组,它输出零行。

#2


2  

puts with an array will print one line per element. No element, no lines.

带有数组的put将为每个元素打印一行。没有元素,没有行。

EDIT: What I just said is documented in your link:

编辑:我刚才所说的都记录在你的链接中:

If called with an array argument, writes each element on a new line.

如果使用数组参数调用,则在新行上写入每个元素。

#3


2  

The link your shared, states:

您共享的链接,说明:

If called with an array argument, writes each element on a new line.

如果使用数组参数调用,则在新行上写入每个元素。

puts []

means, you are calling puts with empty array. i.e. no elements to print. and that's what happened.

意味着,您正在使用空数组调用put。即不需要打印任何元素。这是发生了什么事。

#4


2  

puts arr

is like

就像

arr.each { |e| puts e }

You can do something like this by yourself:

你可以自己做这样的事情:

def p(s)
  if s.respond_to? 'each'
    s.each { |e| p e }
  else
    puts s
  end
end

p 'hello' # prints line with 'hello'
p [] # prints nothing
p [1, 2] # prints 2 lines with 1 and 2

#5


2  

Puts with no arguments has special behaviour - i.e. print new line. In all other cases, it treats all arguments as an array, and maps these arguments to strings using #to_s, and outputs each string on a new line. That's why you get no output when calling puts []. If you want to have a new line in the output, you can either call puts with no arguments (it's obvjous), or use splat operator with empty array, like this: puts *[].

没有参数的put具有特殊的行为——即打印新行。在所有其他情况下,它将所有参数视为一个数组,并使用#to_s将这些参数映射到字符串,并在新行上输出每个字符串。这就是为什么调用put[]时没有输出。如果您想在输出中有一个新的行,您可以不带参数调用put(它是obvjous),或者使用带空数组的splat操作符,如:put *[]。

You can write your own implementation of puts in order to understand things better.

您可以编写自己的put实现,以便更好地理解事情。

  def my_puts(*args)
    STDOUT.write("args is #{args.inspect}\n")
    if args.empty?
      STDOUT.write("\n")
    else
      args.each { |arg| STDOUT.write("#{arg.to_s}\n") }
    end
  end


1.9.3p194 :039 > my_puts
args is []

 => 1 
1.9.3p194 :040 > my_puts []
args is [[]]
[]
 => [[]] 
1.9.3p194 :041 > my_puts *[]
args is []

 => 1 
1.9.3p194 :042 > my_puts 1,2,3
args is [1, 2, 3]
1
2
3
 => [1, 2, 3] 
1.9.3p194 :043 > my_puts [1,2,3]
args is [[1, 2, 3]]
[1, 2, 3]
 => [[1, 2, 3]] 
1.9.3p194 :044 > my_puts *[1,2,3]
args is [1, 2, 3]
1
2
3
 => [1, 2, 3] 

#1


8  

The stated reason is that [] isn't an object

声明的原因是[]不是对象

Stated where?

说明在哪里?

puts has a special handling for arrays. When you pass it an array, it prints each element on a new line. You pass it an array with zero elements, it prints zero lines.

put对数组有特殊的处理。当您将数组传递给它时,它将在新行上打印每个元素。你传递一个零元素的数组,它输出零行。

#2


2  

puts with an array will print one line per element. No element, no lines.

带有数组的put将为每个元素打印一行。没有元素,没有行。

EDIT: What I just said is documented in your link:

编辑:我刚才所说的都记录在你的链接中:

If called with an array argument, writes each element on a new line.

如果使用数组参数调用,则在新行上写入每个元素。

#3


2  

The link your shared, states:

您共享的链接,说明:

If called with an array argument, writes each element on a new line.

如果使用数组参数调用,则在新行上写入每个元素。

puts []

means, you are calling puts with empty array. i.e. no elements to print. and that's what happened.

意味着,您正在使用空数组调用put。即不需要打印任何元素。这是发生了什么事。

#4


2  

puts arr

is like

就像

arr.each { |e| puts e }

You can do something like this by yourself:

你可以自己做这样的事情:

def p(s)
  if s.respond_to? 'each'
    s.each { |e| p e }
  else
    puts s
  end
end

p 'hello' # prints line with 'hello'
p [] # prints nothing
p [1, 2] # prints 2 lines with 1 and 2

#5


2  

Puts with no arguments has special behaviour - i.e. print new line. In all other cases, it treats all arguments as an array, and maps these arguments to strings using #to_s, and outputs each string on a new line. That's why you get no output when calling puts []. If you want to have a new line in the output, you can either call puts with no arguments (it's obvjous), or use splat operator with empty array, like this: puts *[].

没有参数的put具有特殊的行为——即打印新行。在所有其他情况下,它将所有参数视为一个数组,并使用#to_s将这些参数映射到字符串,并在新行上输出每个字符串。这就是为什么调用put[]时没有输出。如果您想在输出中有一个新的行,您可以不带参数调用put(它是obvjous),或者使用带空数组的splat操作符,如:put *[]。

You can write your own implementation of puts in order to understand things better.

您可以编写自己的put实现,以便更好地理解事情。

  def my_puts(*args)
    STDOUT.write("args is #{args.inspect}\n")
    if args.empty?
      STDOUT.write("\n")
    else
      args.each { |arg| STDOUT.write("#{arg.to_s}\n") }
    end
  end


1.9.3p194 :039 > my_puts
args is []

 => 1 
1.9.3p194 :040 > my_puts []
args is [[]]
[]
 => [[]] 
1.9.3p194 :041 > my_puts *[]
args is []

 => 1 
1.9.3p194 :042 > my_puts 1,2,3
args is [1, 2, 3]
1
2
3
 => [1, 2, 3] 
1.9.3p194 :043 > my_puts [1,2,3]
args is [[1, 2, 3]]
[1, 2, 3]
 => [[1, 2, 3]] 
1.9.3p194 :044 > my_puts *[1,2,3]
args is [1, 2, 3]
1
2
3
 => [1, 2, 3]