1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add 3rd person aliases of Symbol#start_with? and Symbol#end_with?

This is the opposite direction of #39152.
This commit is contained in:
Ryuta Kamizono 2020-05-06 00:00:07 +09:00
parent 783a195cb7
commit 62a20c2a64
5 changed files with 68 additions and 0 deletions

View file

@ -1,3 +1,12 @@
* Add 3rd person aliases of `Symbol#start_with?` and `Symbol#end_with?`.
```ruby
:foo.starts_with?("f") # => true
:foo.ends_with?("o") # => true
```
*Ryuta Kamizono*
* Add override of unary plus for `ActiveSupport::Duration`.
`+ 1.second` is now identical to `+1.second` to prevent errors

View file

@ -0,0 +1,3 @@
# frozen_string_literal: true
require "active_support/core_ext/symbol/starts_ends_with"

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
class Symbol
def start_with?(prefix)
to_s.start_with?(prefix)
end unless :a.respond_to?(:start_with?)
def end_with?(suffix)
to_s.end_with?(suffix)
end unless :a.respond_to?(:end_with?)
alias :starts_with? :start_with?
alias :ends_with? :end_with?
end

View file

@ -0,0 +1,28 @@
# frozen_string_literal: true
require_relative "../abstract_unit"
require "active_support/core_ext/symbol"
class SymbolStartsEndsWithTest < ActiveSupport::TestCase
def test_start_end_with
s = :hello
assert s.start_with?("h")
assert s.start_with?("hel")
assert_not s.start_with?("el")
assert s.end_with?("o")
assert s.end_with?("lo")
assert_not s.end_with?("el")
end
def test_starts_ends_with_alias
s = :hello
assert s.starts_with?("h")
assert s.starts_with?("hel")
assert_not s.starts_with?("el")
assert s.ends_with?("o")
assert s.ends_with?("lo")
assert_not s.ends_with?("el")
end
end

View file

@ -1758,6 +1758,20 @@ INFO: The three of them return `nil` for blank receivers.
NOTE: Defined in `active_support/core_ext/string/conversions.rb`.
Extensions to `Symbol`
----------------------
### `starts_with?` and `ends_with?`
Active Support defines 3rd person aliases of `Symbol#start_with?` and `Symbol#end_with?`:
```ruby
:foo.starts_with?("f") # => true
:foo.ends_with?("o") # => true
```
NOTE: Defined in `active_support/core_ext/symbol/starts_ends_with.rb`.
Extensions to `Numeric`
-----------------------