Ruby 2.7 adds Symbol#start_with? and Symbol#end_with? methods

1 minute read

Ruby 2.7 has added new methods Symbol#start_with? and Symbol#end_with?

These methods behave similar to the methods with same name in String class.

Symbol#start_with?

The Symbol#start_with? returns true if the symbol starts with provided prefixes. The provided prefixes can be a string, regex or multiple strings. In case of multiple strings it returns true if any of string is prefix of symbol.

:saeloun.start_with?("sae") => true :saeloun.start_with?("new") => false :saeloun.start_with?("sae", "new") => true :saeloun.start_with?("means", "new") => false :saeloun.start_with?(/SA/i) => true :saeloun.start_with?(/s./) => true :saeloun.start_with?(/ne*/) => false

Symbol#end_with?

Similarly Symbol#end_with? returns true if the symbol ends with provided suffixes. Again the provided suffix arguments can be a string, multiple strings.

:saeloun.end_with?("oun") => true :saeloun.end_with?("new") => false :saeloun.end_with?("oun", "new") => true :saeloun.end_with?("means", "new") => false :saeloun.end_with?(/.n/) => TypeError (no implicit conversion of Regexp into String)