Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

To MRI behaviors / When Regexp receive Symbol objects #1873

Merged
merged 2 commits into from

3 participants

Kenichi Kamiya Don't Add Me To Your Organization a.k.a The Travis Bot Dirkjan Bussink
Kenichi Kamiya
Collaborator

Example

MRI/YARV

ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]

/symbol/ =~ :symbol    # => 0
/symbol/.match :symbol # => #<MatchData "symbol"> 

Rubinius

rubinius 2.0.0dev (1.9.3 af9f288 yyyy-mm-dd JI) [i686-pc-linux-gnu]

/symbol/ =~ :symbol    # => TypeError: Coercion error: :symbol.to_str => String failed
/symbol/.match :symbol # => TypeError: Coercion error: :symbol.to_str => String failed

Suggestion

Cast via Symbol#to_s, just before StringValue()

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged adb46bc into af9f288).

Dirkjan Bussink dbussink merged commit efd14a0 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
17 kernel/common/regexp.rb
View
@@ -67,23 +67,6 @@ def ~
return res ? res.begin(0) : nil
end
- # Returns the index of the first character in the region that
- # matched or nil if there was no match. See #match for returning
- # the MatchData instead.
- def =~(str)
- # unless str.nil? because it's nil and only nil, not false.
- str = StringValue(str) unless str.nil?
-
- match = match_from(str, 0)
- if match
- Regexp.last_match = match
- return match.begin(0)
- else
- Regexp.last_match = nil
- return nil
- end
- end
-
def match_all(str)
start = 0
arr = []
17 kernel/common/regexp18.rb
View
@@ -42,6 +42,23 @@ def match(str)
Regexp.last_match = search_region(str, 0, str.bytesize, true)
end
+ # Returns the index of the first character in the region that
+ # matched or nil if there was no match. See #match for returning
+ # the MatchData instead.
+ def =~(str)
+ # unless str.nil? because it's nil and only nil, not false.
+ str = StringValue(str) unless str.nil?
+
+ match = match_from(str, 0)
+ if match
+ Regexp.last_match = match
+ return match.begin(0)
+ else
+ Regexp.last_match = nil
+ return nil
+ end
+ end
+
def ===(other)
unless other.kind_of?(String)
other = Rubinius::Type.check_convert_type other, String, :to_str
19 kernel/common/regexp19.rb
View
@@ -37,6 +37,7 @@ def match(str, pos=0)
return nil
end
+ str = str.to_s if str.is_a?(Symbol)
str = StringValue(str)
result = search_region(str, pos, str.bytesize, true)
Regexp.last_match = result
@@ -48,6 +49,24 @@ def match(str, pos=0)
end
end
+ # Returns the index of the first character in the region that
+ # matched or nil if there was no match. See #match for returning
+ # the MatchData instead.
+ def =~(str)
+ str = str.to_s if str.is_a?(Symbol)
+ # unless str.nil? because it's nil and only nil, not false.
+ str = StringValue(str) unless str.nil?
+
+ match = match_from(str, 0)
+ if match
+ Regexp.last_match = match
+ return match.begin(0)
+ else
+ Regexp.last_match = nil
+ return nil
+ end
+ end
+
def ===(other)
if other.kind_of? Symbol
other = other.to_s
10 spec/ruby/core/regexp/match_spec.rb
View
@@ -16,6 +16,12 @@
it "returns the index of the first character of the matching region" do
(/(.)(.)(.)/ =~ "abc").should == 0
end
+
+ ruby_version_is "1.9" do
+ it "returns the index too, when argument is a Symbol" do
+ (/(.)(.)(.)/ =~ :abc).should == 0
+ end
+ end
end
describe "Regexp#match" do
@@ -26,6 +32,10 @@
end
ruby_version_is "1.9" do
+ it "returns a MatchData object, when argument is a Symbol" do
+ /(.)(.)(.)/.match(:abc).should be_kind_of(MatchData)
+ end
+
it "matches the input at a given position" do
/./.match("abc", 1).begin(0).should == 1
end
Something went wrong with that request. Please try again.