A Ruby substring example

Ruby substring FAQ: I can't find a Ruby substring method, how can I tell if one Ruby string contains another string?

Solution: I expected the Ruby String class to include a method named substring, but it doesn't. My personal disappointment aside, it does have a method named include? that works the way I expected it to. Here's a simple example, using the irbenvironment to perform a few tests.

>> a = 'hello world'
=> "hello world"

>> a.include? 'ell'
=> true

>> a.include? 'YO'
=> false

If you were looking for a Ruby substring method, I hope that was helpful.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.