Ruby中使用引号括起字符表示字符串,但单引号和双引号有所不同。
1. 单引号所括的内容不会做转义,直接就是字符串的内容。如:
Ruby Code
1
|
puts
'this is a test\n'
|
2. 双引号中,Ruby会做更多事情。
首先Ruby会查找转义符‘\’,并做相应的替换。如:
Ruby Code
1
|
puts
"this is a test\n"
|
其次是表达式求值,对于字符串中的#{expression}会被表达式的值所替换。如:
Ruby Code
1
2 |
title =
'test\n'
puts "this is a #{title}" |