Created
October 21, 2022 01:08
-
-
Save ar-to/cdcc81828dc9b064011155fca26f295d to your computer and use it in GitHub Desktop.
Check methods used to check a string. e.g. Email domain validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Small script to check the difference between methods | |
# that compare strings | |
# | |
# motivation: to check for a domain in an email for use in validation | |
# | |
def check (list, string, regex_string, verbose=true) | |
w_include = 0 | |
w_regex = 0 | |
puts "Check via include?" | |
list.each do |s| | |
r = s.include?(string) | |
w_include += 1 if r | |
puts "#{s} : #{r}" if verbose | |
end | |
puts "Check via regex" | |
list.each do |s| | |
r = s.match?(/#{regex_string}/) | |
w_regex += 1 if r | |
puts "#{s} : #{r}" if verbose | |
end | |
p_include = w_include.to_f / list.count * 100 | |
p_regex = w_regex.to_f / list.count * 100 | |
h_include = { "name" => "With Include?", "Percentage" => p_include} | |
h_regex = { "name" => "With Regex", "Percentage" => p_regex} | |
winner = p_include > p_regex ? h_include : h_regex | |
arbitrary_threshold = 60.to_f | |
but_is_it = winner["name"] if winner["Percentage"] > arbitrary_threshold | |
puts "Percentage of difference:" | |
puts "With include? is #{p_include}%" | |
puts "With regex is #{p_regex}%" | |
puts "The winner is #{but_is_it}" | |
return | |
end | |
list = %w( | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
yourdomain@[email protected] | |
@[email protected] | |
) | |
# run | |
check(list, "@yourdomain", "^(([email protected]).)*$", false) | |
# sample | |
# > check(list, "@yourdomain", "^(([email protected]).)*$", false) | |
# Check via include? | |
# Check via regex | |
# Percentage of difference: | |
# With include? is 83.33333333333334% | |
# With regex is 50.0% | |
# The winner is With Include? | |
# nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment