* [ruby-dev:50862] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
@ 2019-11-13 23:45 ` kamipo
2019-11-14 16:08 ` [ruby-dev:50863] " shevegen
` (6 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: kamipo @ 2019-11-13 23:45 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been reported by kamipo (Ryuta Kamizono).
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348
* Author: kamipo (Ryuta Kamizono)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-dev:50863] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
2019-11-13 23:45 ` [ruby-dev:50862] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include? kamipo
@ 2019-11-14 16:08 ` shevegen
2019-11-17 14:20 ` [ruby-dev:50864] " naruse
` (5 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: shevegen @ 2019-11-14 16:08 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been updated by shevegen (Robert A. Heiler).
I have no particular pro/con opinion. When it comes to class Symbol, though, I believe
that this is is a design decision for matz. I understand that some of the comments
in the other threads are related to rails, but the suggestion here would mean to also
add three methods to class Symbol, so the impact of such a change should be considered,
if there is one (including the semantics and design decisions; remember strange
things such as HashWithIndirectAccess).
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348#change-82683
* Author: kamipo (Ryuta Kamizono)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-dev:50864] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
2019-11-13 23:45 ` [ruby-dev:50862] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include? kamipo
2019-11-14 16:08 ` [ruby-dev:50863] " shevegen
@ 2019-11-17 14:20 ` naruse
2019-11-18 0:12 ` [ruby-dev:50865] " nobu
` (4 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: naruse @ 2019-11-17 14:20 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been updated by naruse (Yui NARUSE).
```
diff --git a/string.c b/string.c
index 554aabad4b..d53fdc89fd 100644
--- a/string.c
+++ b/string.c
@@ -11166,6 +11166,64 @@ sym_swapcase(int argc, VALUE *argv, VALUE sym)
return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym)));
}
+/*
+ * call-seq:
+ * sym.include? other_str -> true or false
+ *
+ * Returns <code>true</code> if <i>str</i> contains the given string or
+ * character.
+ *
+ * :hello.include? "lo" #=> true
+ * :hello.include? "ol" #=> false
+ * :hello.include? ?h #=> true
+ */
+
+static VALUE
+sym_include(VALUE sym, VALUE arg)
+{
+ return rb_str_include(rb_sym2str(sym), arg);
+}
+
+/*
+ * call-seq:
+ * sym.start_with?([prefixes]+) -> true or false
+ *
+ * Returns true if +sym+ starts with one of the +prefixes+ given.
+ * Each of the +prefixes+ should be a String or a Regexp.
+ *
+ * :hello.start_with?("hell") #=> true
+ * :hello.start_with?(/H/i) #=> true
+ *
+ * # returns true if one of the prefixes matches.
+ * :hello.start_with?("heaven", "hell") #=> true
+ * :hello.start_with?("heaven", "paradise") #=> false
+ */
+
+static VALUE
+sym_start_with(int argc, VALUE *argv, VALUE sym)
+{
+ return rb_str_start_with(argc, argv, rb_sym2str(sym));
+}
+
+/*
+ * call-seq:
+ * sym.end_with?([suffixes]+) -> true or false
+ *
+ * Returns true if +sym+ ends with one of the +suffixes+ given.
+ *
+ * :hello.end_with?("ello") #=> true
+ *
+ * # returns true if one of the +suffixes+ matches.
+ * :hello.end_with?("heaven", "ello") #=> true
+ * :hello.end_with?("heaven", "paradise") #=> false
+ */
+
+static VALUE
+sym_end_with(int argc, VALUE *argv, VALUE sym)
+{
+ return rb_str_end_with(argc, argv, rb_sym2str(sym));
+}
+
/*
* call-seq:
* sym.encoding -> encoding
@@ -11452,5 +11510,9 @@ Init_String(void)
rb_define_method(rb_cSymbol, "capitalize", sym_capitalize, -1);
rb_define_method(rb_cSymbol, "swapcase", sym_swapcase, -1);
+ rb_define_method(rb_cSymbol, "include?", sym_include, 1);
+ rb_define_method(rb_cSymbol, "start_with?", sym_start_with, -1);
+ rb_define_method(rb_cSymbol, "end_with?", sym_end_with, -1);
+
rb_define_method(rb_cSymbol, "encoding", sym_encoding, 0);
}
diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb
index d657f1aae6..d3a3af508f 100644
--- a/test/ruby/test_symbol.rb
+++ b/test/ruby/test_symbol.rb
@@ -568,4 +568,34 @@ def ==(obj)
puts :a == :a
RUBY
end
+
+ def test_start_with?
+ assert_equal(true, :hello.start_with?("hel"))
+ assert_equal(false, :hello.start_with?("el"))
+ assert_equal(true, :hello.start_with?("el", "he"))
+
+ bug5536 = '[ruby-core:40623]'
+ assert_raise(TypeError, bug5536) {:str.start_with? :not_convertible_to_string}
+
+ assert_equal(true, :hello.start_with?(/hel/))
+ assert_equal("hel", $&)
+ assert_equal(false, :hello.start_with?(/el/))
+ assert_nil($&)
+ end
+
+ def test_end_with?
+ assert_equal(true, :hello.end_with?("llo"))
+ assert_equal(false, :hello.end_with?("ll"))
+ assert_equal(true, :hello.end_with?("el", "lo"))
+
+ bug5536 = '[ruby-core:40623]'
+ assert_raise(TypeError, bug5536) {:str.end_with? :not_convertible_to_string}
+ end
+
+ def test_include?
+ assert_include(:foobar, ?f)
+ assert_include(:foobar, "foo")
+ assert_not_include(:foobar, "baz")
+ assert_not_include(:foobar, ?z)
+ end
end
```
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348#change-82704
* Author: kamipo (Ryuta Kamizono)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-dev:50865] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
` (2 preceding siblings ...)
2019-11-17 14:20 ` [ruby-dev:50864] " naruse
@ 2019-11-18 0:12 ` nobu
2019-11-18 3:42 ` [ruby-dev:50866] " duerst
` (3 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: nobu @ 2019-11-18 0:12 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been updated by nobu (Nobuyoshi Nakada).
naruse (Yui NARUSE) wrote:
> ```diff
> + * sym.include? other_str -> true or false
> + *
> + * Returns <code>true</code> if <i>str</i> contains the given string or
`str` should be `sym`.
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348#change-82708
* Author: kamipo (Ryuta Kamizono)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-dev:50866] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
` (3 preceding siblings ...)
2019-11-18 0:12 ` [ruby-dev:50865] " nobu
@ 2019-11-18 3:42 ` duerst
2019-11-28 9:08 ` [ruby-dev:50871] " matz
` (2 subsequent siblings)
7 siblings, 0 replies; 8+ messages in thread
From: duerst @ 2019-11-18 3:42 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been updated by duerst (Martin Dürst).
I personally think that we should be restrictive in adding new methods to `Symbol`. Otherwise, this very quickly becomes a slippery slope, and the distinction between `Symbol` and `String` becomes less and less clear.
I was personally surprised when I implemented feature #10085 that `Symbol` had `#upcase` and friends, I wouldn't have guessed that.
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348#change-82710
* Author: kamipo (Ryuta Kamizono)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-dev:50871] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
` (4 preceding siblings ...)
2019-11-18 3:42 ` [ruby-dev:50866] " duerst
@ 2019-11-28 9:08 ` matz
2019-11-28 9:38 ` [ruby-dev:50872] " hanmac
2019-11-28 16:30 ` [ruby-dev:50873] " naruse
7 siblings, 0 replies; 8+ messages in thread
From: matz @ 2019-11-28 9:08 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been updated by matz (Yukihiro Matsumoto).
I am OK with adding `start_with?` and `end_with?`.
I am a bit reluctant with adding `include?` since Symbol does not include any character. Persuade me further if you really need `Symbol#include?`.
Matz.
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348#change-82859
* Author: kamipo (Ryuta Kamizono)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-dev:50872] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
` (5 preceding siblings ...)
2019-11-28 9:08 ` [ruby-dev:50871] " matz
@ 2019-11-28 9:38 ` hanmac
2019-11-28 16:30 ` [ruby-dev:50873] " naruse
7 siblings, 0 replies; 8+ messages in thread
From: hanmac @ 2019-11-28 9:38 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been updated by Hanmac (Hans Mackowiak).
there is also `symbol[string]` which can be used as replace for Symbol#include?
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348#change-82860
* Author: kamipo (Ryuta Kamizono)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [ruby-dev:50873] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
` (6 preceding siblings ...)
2019-11-28 9:38 ` [ruby-dev:50872] " hanmac
@ 2019-11-28 16:30 ` naruse
7 siblings, 0 replies; 8+ messages in thread
From: naruse @ 2019-11-28 16:30 UTC (permalink / raw)
To: ruby-dev
Issue #16348 has been updated by naruse (Yui NARUSE).
duerst (Martin Dürst) wrote:
> I personally think that we should be restrictive in adding new methods to `Symbol`. Otherwise, this very quickly becomes a slippery slope, and the distinction between `Symbol` and `String` becomes less and less clear.
>
> I was personally surprised when I implemented feature #10085 that `Symbol` had `#upcase` and friends, I wouldn't have guessed that.
As far as I remember, once Matz tried and gave up to unify String and Symbol, he said Symbol should have similar set of methods while people needed.
After a decade some but small number of methods are added even if including this.
I think there's no worry.
----------------------------------------
Feature #16348: Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include?
https://bugs.ruby-lang.org/issues/16348#change-82866
* Author: kamipo (Ryuta Kamizono)
* Status: Closed
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When replacing #match? to #start_with?, #end_with?, and #include? for some reason (address to https://bugs.ruby-lang.org/issues/13083 etc), we frequently hit missing Symbol#start_with?, Symbol#end_with?, and Symbol#include? in spite of Symbol#match? exists.
https://github.com/rails/rails/commit/63256bc5d7dd77b2cce82df46c53249dab2dc2a8
https://github.com/rails/rails/commit/a8e812964d711fa03843e76ae50f5ff81cdc9e00
Is this inconsistency intentional?
If not so, Symbol#start_with?, Symbol#end_with?, and Symbol#include? prevents such like an issue.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2019-11-28 16:30 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <redmine.issue-16348.20191113234525@ruby-lang.org>
2019-11-13 23:45 ` [ruby-dev:50862] [Ruby master Feature#16348] Proposal: Symbol#start_with?, Symbol#end_with?, and Symbol#include? kamipo
2019-11-14 16:08 ` [ruby-dev:50863] " shevegen
2019-11-17 14:20 ` [ruby-dev:50864] " naruse
2019-11-18 0:12 ` [ruby-dev:50865] " nobu
2019-11-18 3:42 ` [ruby-dev:50866] " duerst
2019-11-28 9:08 ` [ruby-dev:50871] " matz
2019-11-28 9:38 ` [ruby-dev:50872] " hanmac
2019-11-28 16:30 ` [ruby-dev:50873] " naruse
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).