简体   繁体   English

如何在 ruby 中进行多行打印?

[英]How can i do Multi Line printing in ruby?

i want to ask something.我想问一些事情。 I am learning Ruby programming language newly.我正在学习 Ruby 编程语言。 Im writing a application.我正在写一个应用程序。 However, my question is "How can i multi line print something?"但是,我的问题是“我怎样才能多行打印一些东西?” like how it is:就像它是这样的:

Python code: Python代码:


print("""

Hey
Hello
Hi

""")

How can i do this with ruby?我怎么能用 ruby 做到这一点?

If the string doesn't contain quotation marks you can do it like this:如果字符串不包含引号,您可以这样做:

text = "this is
a
multi-line string"

Another option would be a here document (heredoc) syntax:另一种选择是此处的文档(heredoc)语法:

text = <<~HEREDOC
this is
a
multi-line string
HEREDOC

You can also use a percent string:您还可以使用百分比字符串:

text = %q(
this is
a
multi-line string
)

Three methods that immediately spring to mind:立即想到 spring 的三种方法:

# heredoc format
print <<~MESSAGE
          I
          Am
          Multiline
MESSAGE

# array join
print %w|I am Multiline|.join("\n")

# map
%w|I am Multiline|.map {|x| puts x  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM