Ruby 符号 slice函数
Symbol#slice() : slice() 是 Symbol 类的一个方法,它对符号对象进行切片并返回字符值。
语法。Symbol.slice()
参数。符号值
返回:对符号对象进行切片,并返回字符值。
例子 #1 :
# Ruby code for Symbol.slice() method
# declaring Symbol
a = :aBcDeF
# declaring Symbol
b = :"\u{e4 f6 fc}"
# declaring Symbol
c = :ABCDEF
# Symbol
puts "Symbol a : #{a}\n\n"
puts "Symbol b : #{b}\n\n"
puts "Symbol c : #{c}\n\n\n\n"
# slice form
puts "Symbol a slice form : #{a.slice(1)}\n\n"
puts "Symbol b slice form : #{b.slice(1)}\n\n"
puts "Symbol c slice form : #{c.slice(3)}\n\n"
输出:
Symbol a : aBcDeF
Symbol b : äöü
Symbol c : ABCDEF
Symbol a slice form : B
Symbol b slice form : ö
Symbol c slice form : D
例子 #2 :
# Ruby code for Symbol.slice() method
# declaring Symbol
a = :geeks
# declaring Symbol
b = :"\u{e5 f6 f3}"
# declaring Symbol
c = :GEEKS
# Symbol
puts "Symbol a : #{a}\n\n"
puts "Symbol b : #{b}\n\n"
puts "Symbol c : #{c}\n\n\n\n"
# slice form
puts "Symbol a slice form : #{a.slice(1)}\n\n"
puts "Symbol b slice form : #{b.slice(1)}\n\n"
puts "Symbol c slice form : #{c.slice(3)}\n\n"
输出:
Symbol a : geeks
Symbol b : åöó
Symbol c : GEEKS
Symbol a slice form : e
Symbol b slice form : ö
Symbol c slice form : K