红宝石 |字符串转储方法
dump是 Ruby 中的 String 类方法,用于生成给定字符串的一个版本,其中所有非打印字符由 \nnn 表示法替换,并且所有特殊字符都被转义。
Syntax: str.dump
Parameters: Here, str is the given string.
Returns: A new string with all non-printing characters replaced by \nnn notation and all special characters escaped.
示例 1:
# Ruby program to demonstrate
# the dump method
# Taking a string and
# using the method
str = "ruby \n ''".dump
puts str.dump
输出:
"\"ruby \\n ''\""
示例 2:
# Ruby program to demonstrate
# the dump method
# Taking a string and
# using the method
str = "yumy \n\n\n ''".dump
puts str.dump
输出:
"\"yumy \\n\\n\\n ''\""