红宝石 |正则表达式 hash()函数
Regexp#force_encoding?() : force_encoding?()是一个 Regexp 类方法,它根据正则表达式的文本和选项返回哈希值。
Syntax: Regexp.hash()
Parameter: Regexp values
Return: the hash based on the text and options of this regular expression.
示例 #1:
# Ruby code for Regexp.hash() method
# declaring Regexp value
reg_a = /a/
# declaring Regexp value
reg_b = /geeks/
# declaring Regexp value
reg_c = /a/
# hash method
puts "Regexp hash form : #{reg_a.hash}\n\n"
puts "Regexp hash form : #{reg_b.hash}\n\n"
puts "Regexp hash form : #{reg_c.hash}\n\n"
输出 :
Regexp hash form : -1704400854280844509
Regexp hash form : -3140932202593119845
Regexp hash form : -1704400854280844509
示例 #2:
# Ruby code for Regexp.hash() method
# declaring Regexp value
reg_a = /geeks/
# declaring Regexp value
reg_b = /problem/
# declaring Regexp value
reg_c = /code/
# hash method
puts "Regexp hash form : #{reg_a.hash}\n\n"
puts "Regexp hash form : #{reg_b.hash}\n\n"
puts "Regexp hash form : #{reg_c.hash}\n\n"
输出 :
Regexp hash form : -429624664738525607
Regexp hash form : -2782281071524532422
Regexp hash form : -3545766771755419715