您可以使用 puts
、print
和 p
等打印语句在 Ruby 中将输出显示到屏幕上。这些方法的工作方式如下:
方法 | 描述 |
---|---|
puts |
打印后附加一个换行符。 |
print |
打印时不附加换行符。 |
p |
以技术方式打印,便于调试。 |
让我们逐一查看这些方法。
Ruby puts 方法
Ruby 中的 puts
方法会打印内容并在末尾自动添加一个换行符。例如,
# Print a sentence to the screen using puts
puts "Ruby tutorials for beginners!"
# Print another sentence to the screen
puts "Brought to you by Programiz!"
输出
Ruby tutorials for beginners! Brought to you by Programiz!
在这里,我们使用了两个 puts
语句来打印两个句子。
正如你所见,第二个句子打印在新的一行上。
这是因为 puts
在其打印的文本末尾添加了一个换行符。因此,下一个打印语句将打印在新的一行上。
Ruby print 方法
print
方法的工作方式类似,但不添加换行符。这意味着后续输出会出现在同一行上。例如,
# Print a sentence to the screen using print
print "Ruby tutorials for beginners!"
# Print another sentence to the screen
print "Brought to you by Programiz!"
# Output: Ruby tutorials for beginners!Brought to you by Programiz!
在这里,我们使用了两个 print
语句来打印两个句子。
由于 print
不会将光标移到下一行,因此第二个消息会紧跟在第一个之后显示。
使用 print 方法添加换行符
您可以通过使用换行符 \n
手动在 print 中插入换行符。例如,
# Add a newline character at the end of the string
print "Ruby tutorials for beginners!\n"
print "Brought to you by Programiz!"
输出
Ruby tutorials for beginners! Brought to you by Programiz!
Ruby p 方法
p
方法通常用于调试。它以对开发者友好的方式打印值。
- 字符串会显示在引号中。
- 其他值(如数字、
nil
、true
、false
等)将按原样显示。
示例
# Print a string
p "Ruby tutorials for beginners!"
# Print a number
p 34
# Print 'nil' value
p nil
# Print boolean values 'true' and 'false'
p true
p false
输出
"Ruby tutorials for beginners!" 34 nil true false
正如你所见,字符串值会用引号打印,而其他值则不加引号打印。
更多关于 Ruby p 方法
p
方法非常适合调试,因为它会打印对象的内部表示。
在 Ruby 中,inspect
方法提供对象的内部表示。
因此,p
方法打印调用对象上的 .inspect
的结果,这是 Ruby 显示内部、对开发者友好的数据表示方式。例如,
greeting = "Hello, World"
# Print greeting using p
p greeting
# Print greeting using inspect
puts greeting.inspect
输出
"Hello, World" "Hello, World"
正如你所见,p greeting
的输出与 puts greeting.inspect
的输出相同。因此,使用 p
打印等同于
puts obj.inspect
在这里,obj
是任何对象,如字符串、数字、布尔值、数组、类实例等。
示例 1:比较不同的打印语句
让我们用一个例子来比较所有三个打印语句。
# Use puts to print a string
puts "Hello, World!"
# Use print to display a string
print "Hello, World! "
# Use p to print a string
p "Hello, World!"
输出
Hello, World! Hello, World! "Hello, World!"
这里,
puts
会添加换行符。print
会将输出保留在同一行。p
会清晰地显示值,包括引号。
示例 2:打印数组
Ruby 在使用不同的显示方法打印数组时有一些有趣的差异。让我们通过一个例子来演示一下。
my_array = [1, 2, 3]
puts "Printing Array Using puts:"
puts my_array
print "\nPrinting Array Using print: "
print my_array
print "\n\nPrinting Array Using p: "
p my_array
输出
Printing Array Using puts: 1 2 3 Printing Array Using print: [1, 2, 3] Printing Array Using p: [1, 2, 3]
正如你所见,print
和 p
方法以数组表示法显示数组。
另一方面,puts
方法在单独的行上显示每个数组元素。
打印多个项
Ruby 允许您使用单个语句打印多个项。您可以通过以下两种方式之一实现:
- 通过使用逗号将多个参数馈送到方法中。
- 通过使用
+
运算符连接字符串。
注意:如果您不确定“参数”是什么意思,请不要担心——当我们在 方法中介绍它时,您很快就会学到。
接下来,让我们探讨这些技术。
使用逗号打印多个项
您可以使用逗号分隔要打印的项。例如,
name = "Peter"
age = 29
puts "Commas with puts:"
puts "Hello ", name, age
puts "\nCommas with print:"
print "Hello ", name, age
puts "\n\nCommas with p:"
p "Hello ", name, age
输出
Commas with puts: Hello Peter 29 Commas with print: Hello Peter29 Commas with p: "Hello " "Peter" 29
在这里,我们使用不同的打印语句打印了字符串 "Hello "
和变量 name
和 age
。正如您所见,
puts
和p
将各项显示在单独的行上。print
将各项显示在同一行上,并且它们之间没有任何空格。
使用 + 运算符打印多个字符串
+
运算符用于连接(连接)您要打印的不同字符串。这些字符串将连接在同一行上,无论您使用的是 puts 还是 print。
注意: +
运算符仅连接字符串。如果您尝试将其与数字一起使用,Ruby 将会引发错误(当一个操作数是字符串时),或者执行算术加法(当两个都是数字时)。
示例
name = "Peter"
puts "Concatenation with puts:"
puts "Hello " + name
puts "\nConcatenation with print:"
print "Hello " + name
puts "\n\nConcatenation with p:"
p "Hello " + name
输出
Concatenation with puts: Hello Peter Concatenation with print: Hello Peter Concatenation with p: "Hello Peter"
在这里,我们使用 +
运算符连接了字符串 "Hello "
和字符串变量 name
。正如您所见,对象连接在同一行上。
连接仅适用于字符串
只有字符串可以连接。您也不能将字符串与另一种类型的数据连接。例如,
age = 29
# Invalid: Joining a string and a number
puts "Age: " + age
# Output: no implicit conversion of Integer into String (TypeError)
# Invalid: Concatenating boolean values
print true + false
# Output: undefined method '+' for true (NoMethodError)
如果所有操作数都是数字,Ruby 将会算术地将它们相加,而不是连接它们。
age = 29
# + will add the numbers instead of joining them
puts 12 + age
print 12 + age
p 12 + age
输出
41 4141
在这里,程序打印 12 和 age
变量中存储的值的和。结果是 12 + 29
,即 41。
您可以通过将不同类型转换为字符串来修复失败的连接问题。为此,请使用 to_s
方法。
age = 29
# Valid: age has been converted to string
puts "Age: " + age.to_s
# Valid: Both boolean values are now strings
puts true.to_s + false.to_s
# Concatenate two numbers by converting them to strings
puts 12.to_s + age.to_s
输出
Age: 29 truefalse 1229
字符串插值:打印文本和变量
在 Ruby 中,您可以使用 #{}
的插值将变量直接插入到字符串中。这仅在双引号中有效。例如,
# Create name variable
name = "Ash Ketchum"
# Create age variable
age = 10
# Print the variables inside a string
puts "Hello, #{name}! You are #{age} years old."
# Output: Hello, Ash Ketchum! You are 10 years old.
这里,
#{name}
- 将name
变量的值插入到打印的字符串中。#{age}
- 将age
变量的值插入到打印的字符串中。
注意:在字符串插值中,变量的值将作为字符串插入。因此,即使 age
是一个 number
类型的变量,Ruby 在插值过程中也会自动将其转换为字符串。