简体   繁体   中英

How can i do Multi Line printing in ruby?

i want to ask something. I am learning Ruby programming language newly. Im writing a application. However, my question is "How can i multi line print something?" like how it is:

Python code:


print("""

Hey
Hello
Hi

""")

How can i do this with 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:

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:

# 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  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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