regex - Ruby: matching a string against another object that may be a string or regexp -
i'm writing routine compares string against list of objects, each of may either string or regexp. there elegant, ruby-centric way handle this?
for now, i'm doing this:
def compare(str, thelist) thelist.any? |item| case str when item true else false end end end if compare("the string testing", ['i not', /string/i]) # got match
this seems work enough, feels bit hackerish , long winded tastes, i'm wondering if there better way it. (i'm not interested in using instance_of? - came solution because instance_of? ugly.)
working ruby 2.2.2
thanks in advance...
this shortened version of approach:
def compare(str, thelist) thelist.any? { |item| item.match(str) } end
Comments
Post a Comment