Ruby 符号 match函数
Symbol#match() : match() 是 Symbol 类的一个方法,它用符号来匹配模式。
语法。Symbol.match()
参数。符号值
返回:位置 – 如果模式与符号匹配,否则返回nil
例子 #1 :
# Ruby code for Symbol.match() 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"
# match form
puts "Symbol a match form : #{a.match(/D/)}\n\n"
puts "Symbol b match form : #{b.match(/o/)}\n\n"
puts "Symbol c match form : #{c.match(/C/)}\n\n"
输出:
Symbol a : aBcDeF
Symbol b : äöü
Symbol c : ABCDEF
Symbol a match form : 3
Symbol b match form :
Symbol c match form : 2
例子 #2 :
# Ruby code for Symbol.match() 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"
# match form
puts "Symbol a match form : #{a.match(/g/)}\n\n"
puts "Symbol b match form : #{b.match(/a/)}\n\n"
puts "Symbol c match form : #{c.match(/EE/)}\n\n"
输出:
Symbol a : geeks
Symbol b : åöó
Symbol c : GEEKS
Symbol a match form : 0
Symbol b match form :
Symbol c match form : 1