I have this statement:
string_tokens[-1].ends_with?(",") || string_tokens[-1].ends_with?("-") || string_tokens[-1].ends_with?("&")
I would like to put all the tokens (",", "-", "&") into a constant and simplify the above to ask, "does the string end with any of these characters", but I'm not sure how to do that.
Yes.
CONST = %w|, - &|.freeze
string_tokens[-1].end_with?(*CONST)
Usage:
'test,'.end_with?(*CONST)
#=> true
'test&'.end_with?(*CONST)
#=> true
'test-'.end_with?(*CONST)
#=> true
You use * (splat operator) to pass multiple args to the String#end_with?, because it accepts multiple.