Ruby | Regexp hash() function

Regexp#force_encoding?() : force_encoding?() is a Regexp class method which returns the hash based on the text and options of this regular expression.

Syntax: Regexp.hash()

Parameter: Regexp values



Return: the hash based on the text and options of this regular expression.

Example #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

Regexp hash form : -1704400854280844509

Regexp hash form : -3140932202593119845

Regexp hash form : -1704400854280844509

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

Regexp hash form : -429624664738525607

Regexp hash form : -2782281071524532422

Regexp hash form : -3545766771755419715



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.