打印Hello World! 在Ruby中 (Printing Hello World! In Ruby)
To print text (Hello World! Or anything else), we use following two methods,
要打印文本(Hello World!或其他),我们使用以下两种方法,
puts : The word "puts" stands for put string. It adds a newline after the string.
puts :“ puts”一词代表放置字符串。 它在字符串后添加换行符。
print : The functionality of this method is same as of puts but it does not adds newlines implicitly like puts.
print :此方法的功能与puts相同,但不会像puts那样隐式添加换行符。
Variables used: No variables are required as we only have to print the statements. We don't have to store anything.
使用的变量:不需要变量,因为我们只需要打印语句。 我们不必存储任何东西。
Ruby代码打印“ Hello World!” (Ruby code to print "Hello World!")
=begin
Ruby program to print Hello World.
=end
puts "Hello World!"
print "Hello World!"
puts "Hello World!"
Output
输出量
Hello World!
Hello World!Hello World!
Code explanation:
代码说明:
The code illustrates an example to print a string "Hello World!" in Ruby programming language. This uses the puts and print methods to print the string and output "Hello world" using these methods.
该代码说明了打印字符串“ Hello World!”的示例。 用Ruby编程语言编写 。 这使用puts和print方法来打印字符串,并使用这些方法输出“ Hello world” 。
You can observe in the output that the print method is not providing a newline and two statements are printing together. Though, both the methods have the same functionality but still use them wisely.
您可以在输出中观察到print方法未提供换行符,并且两个语句一起打印。 尽管这两种方法都具有相同的功能,但是仍然明智地使用它们。
翻译自: https://www.includehelp.com/ruby/print-hello-world.aspx