Ruby 符号 to_sym函数
Symbol#to_sym() : to_sym() 是 Symbol 类的一个方法,用于返回符号对象的符号表示。
语法。符号.to_sym()
参数。符号值
返回:符号对象的符号表示法。
例子 #1 :
# Ruby code for Symbol.to_sym() method
# declaring Symbol
a = :aBcDeF
# declaring Symbol
b = :"\u{e4 f6 fc}"
# declaring Symbol
c = :ABCDEF
# to_sym form
puts "Symbol a to_sym form : #{a.to_sym}\n\n"
puts "Symbol b to_sym form : #{b.to_sym}\n\n"
puts "Symbol c to_sym form : #{c.to_sym}\n\n"
输出:
Symbol a to_sym form : aBcDeF
Symbol b to_sym form : äöü
Symbol c to_sym form : ABCDEF
例子 #2 :
# Ruby code for Symbol.to_sym() method
# declaring Symbol
a = :geeks
# declaring Symbol
b = :"\u{e5 f6 f3}"
# declaring Symbol
c = :GEEKS
# to_sym form
puts "Symbol a to_sym form : #{a.to_sym}\n\n"
puts "Symbol b to_sym form : #{b.to_sym}\n\n"
puts "Symbol c to_sym form : #{c.to_sym}\n\n"
输出:
Symbol a to_sym form : geeks
Symbol b to_sym form : åöó
Symbol c to_sym form : GEEKS