rspec/rspec-expectations — pull request #135 by jeremywadsack (2012-04-08 21:58:54+00:00)
Title: Add matchers start_with end_with

This pull request provides a pair of new matchers `start_with` and `end_with` that are basically sugar to make specs more readable. They work with strings and arrays (and basically anything else that acts like a collection).

```ruby
"A test string".should start_with 'A test'
[1, 2, 3].should start_with 1
[1, 2, 3].should end_with [2, 3]
```

I recognize that these are somewhat specialized and have noted that most built-in matchers are very, very generalized, but I found we were using a custom matcher like this often in our specs and thought it would be useful for others.

**Reasoning**

Currently, under Rails with ActiveSupport you can use the existing `starts_with?` and `ends_with?` methods to match the beginning and end of strings but they have some drawbacks. If used on the expected value then a failure reports an unhelpful message:

```ruby
"A test string".starts_with?("Something").should be_true
# ==> "expected true but got false"
```

When used as a predicate, it doesn't quite read like english and is awkward:

```ruby
"A test string".should be_starts_with("A test")
```

Finally, while we are using this for our Rails apps that have [ActiveSupport](http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/StartsEndsWith.html) for `starts_with?` and `ends_with?`, this implementation work independently of ActiveSupport so can be used outside if a Rails app as the rest of Rspec does.

Open to feedback as to whether you think these are valuable as built-in specs.