Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用“puts”显示多行的首选样式`_Ruby_Puts - Fatal编程技术网

Ruby 使用“puts”显示多行的首选样式`

Ruby 使用“puts”显示多行的首选样式`,ruby,puts,Ruby,Puts,我知道有几种不同的方法可以将put语句组合在一起。但更重要的是,我正在尝试确定是否有一种普遍接受/首选的风格(我只能挖掘出其他人的聪明方式,但没有真正的参考首选风格) 我见过这样的事情: puts "This", "is", "fairly", "easy" # Each word goes on it's own line 或许: puts ["This", "seems", "convoluted"].join("\n") # Each word goes on it's own lin

我知道有几种不同的方法可以将
put
语句组合在一起。但更重要的是,我正在尝试确定是否有一种普遍接受/首选的风格(我只能挖掘出其他人的聪明方式,但没有真正的参考首选风格)

我见过这样的事情:

puts "This", "is", "fairly", "easy"  # Each word goes on it's own line
或许:

puts ["This", "seems", "convoluted"].join("\n") # Each word goes on it's own line
或者一种“丑陋”的方式:

或者简单地说:

puts "This"
puts "Seems" 
puts "Straightforward."

对我来说,使用最后一种方法是最有意义的,但我只是好奇,在处理这样的多行输出时,是否有一种通用/首选的方法;DR偏好和可读性

我浏览了以下ruby风格指南:

它们都没有提到打印多个puts语句的任何特定或首选方法

我认为这是因地制宜的。在这种情况下,什么更具可读性?如果有几个长puts语句作为简单字符串,请将它们拆分为几行上的单独puts语句

puts "Something very longgggggggggggg"
puts "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an..."
puts  "unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing..."
如果出于某种原因将单独的语句存储在变量中,只需将它们全部放在一行:
将这个、那个、那个东西放在一起


对于在方法中存储puts语句,您可能需要在控制台中打印几行。例如,当制作一个用户将通过终端与之交互和使用的程序时,您可能希望在方法中存储某些语句,并在用户调用时将其打印出来(即打印出指令或打印出可用的命令).

如果要打印的行足够短,可以放在源代码中的一行上,那么我会选择您的第一个选项:

puts "This", "is", "fairly", "easy"
如果它们很长,那么我会使用一个heredoc:

puts <<_.unindent
  This Blah Blah ...
  Seems Blah Blah ...
  Straightforward. Blah Blah ...
_

put让其他人知道
unindent
似乎是一个Ruby gem,需要先安装它,然后这种风格才能工作,这可能会有所帮助。
puts <<_.unindent
  This Blah Blah ...
  Seems Blah Blah ...
  Straightforward. Blah Blah ...
_