未验证 提交 39e4796b 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #39155 from kamipo/symbol_starts_ends_with

Add 3rd person aliases of `Symbol#start_with?` and `Symbol#end_with?`
* Deprecate `starts_with?` and `ends_with?` for String core extensions.
Use the native `start_with?` and `end_with?` instead.
* 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*
......
# frozen_string_literal: true
class String
alias_method :starts_with?, :start_with?
alias_method :ends_with?, :end_with?
deprecate starts_with?: :start_with?
deprecate ends_with?: :end_with?
alias :starts_with? :start_with?
alias :ends_with? :end_with?
end
# frozen_string_literal: true
require "active_support/core_ext/symbol/starts_ends_with"
# 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
......@@ -239,15 +239,15 @@ def test_ord
assert_equal 97, "abc".ord
end
def test_deprecated_starts_ends_with_alias
def test_starts_ends_with_alias
s = "hello"
assert_deprecated { assert s.starts_with?("h") }
assert_deprecated { assert s.starts_with?("hel") }
assert_deprecated { assert_not s.starts_with?("el") }
assert s.starts_with?("h")
assert s.starts_with?("hel")
assert_not s.starts_with?("el")
assert_deprecated { assert s.ends_with?("o") }
assert_deprecated { assert s.ends_with?("lo") }
assert_deprecated { assert_not s.ends_with?("el") }
assert s.ends_with?("o")
assert s.ends_with?("lo")
assert_not s.ends_with?("el")
end
def test_string_squish
......
# 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
......@@ -1212,6 +1212,17 @@ The `inquiry` method converts a string into a `StringInquirer` object making equ
NOTE: Defined in `active_support/core_ext/string/inquiry.rb`.
### `starts_with?` and `ends_with?`
Active Support defines 3rd person aliases of `String#start_with?` and `String#end_with?`:
```ruby
"foo".starts_with?("f") # => true
"foo".ends_with?("o") # => true
```
NOTE: Defined in `active_support/core_ext/string/starts_ends_with.rb`.
### `strip_heredoc`
The method `strip_heredoc` strips indentation in heredocs.
......@@ -1747,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`
-----------------------
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册