4

I just learned about regular expressions, and I understand that they're a pattern used to search for a match in a given string.

I understand how using (/[aeiou]/) with the start_with? method works. The regex (/[aeiou]/) is a class of characters that I am checking for in the given string. And more specifically, I am checking to see if the given string starts with any of those characters in the regex.

However, when I try to use the same pattern with the end_with? method, I get a TypeError and it seems that the regex cannot be used.

Why is this? To me, both of these methods seem to share a very similar documentation in the Ruby guides.

enter image description here

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • You can usually copy text from the console with CTRL + SHIFT + C and then paste and format it here. It's better than posting screenshots – Viktor Oct 17 '19 at 18:33
  • Because it is not implemented. Anway, it is always very easy to search at the end of the line: `/(?:this|or|that)\z/` or `/.*(?:this|or|that)\z/m` – Wiktor Stribiżew Oct 17 '19 at 18:37
  • Maybe a dupe of [How to see if a string ends with one of multiple characters?](https://stackoverflow.com/questions/42537226/how-to-see-if-a-string-ends-with-one-of-multiple-characters) – Wiktor Stribiżew Oct 17 '19 at 18:46
  • 3
    The documentation of both methods differs in that. From the version [2.6.1](https://ruby-doc.org/core-2.6.1/String.html#method-i-start_with-3F) `start_with?` adds the way to use a regular expression as the prefixes argument, but not `end_with?`. – Sebastián Palma Oct 17 '19 at 18:52
  • @SebastianPalma So start_with? can use a regex and end_with? can't use a regex? –  Oct 17 '19 at 19:23
  • Yes, that's it @Karen. Same as Wiktor added, it's not implemented. – Sebastián Palma Oct 17 '19 at 19:25
  • @SebastianPalma 2.5 actually ([see the `NEWS` file](https://docs.ruby-lang.org/en/2.5.0/NEWS.html#label-Core+classes+updates+-28outstanding+ones+only-29)). – cremno Oct 17 '19 at 19:34
  • If so, it's not documented https://ruby-doc.org/core-2.5.0/String.html#method-i-start_with-3F @cremno. – Sebastián Palma Oct 17 '19 at 19:40
  • 3
    Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers. You can edit your question to add the code in the body of your question. For easy formatting use the `{}` button to mark blocks of code, or indent with four spaces for the same effect. The contents of a **screenshot can’t be searched, run as code, or copied and edited to create a solution.** – tadman Oct 17 '19 at 19:53
  • Please do NOT use a screen shot to present output or code. We can't copy/paste to test your work and have to type it in, potentially causing errors, but definitely wasting time. Instead copy, then paste and correctly format the example code and output into the question. – the Tin Man Oct 17 '19 at 20:01

1 Answers1

6

Usually the point of start_with? or end_with? is to provide an alternative to using a regular expression. That is the following are similar in intent:

"example".start_with?("ex")
"example".match?(/\Aex/)

Where if you want to use a regular expression then use match or match? which is available in newer versions of Ruby.

Just convert your regular expression to be anchored:

str.start_with?(/\A[aeiou]/)
tadman
  • 208,517
  • 23
  • 234
  • 262