Skip to content
This repository

To MRI behaviors / When Regexp receive Symbol objects #1873

Merged
merged 2 commits into from almost 2 years ago

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
@@ -67,23 +67,6 @@ def ~
67 67 return res ? res.begin(0) : nil
68 68 end
69 69
70   - # Returns the index of the first character in the region that
71   - # matched or nil if there was no match. See #match for returning
72   - # the MatchData instead.
73   - def =~(str)
74   - # unless str.nil? because it's nil and only nil, not false.
75   - str = StringValue(str) unless str.nil?
76   -
77   - match = match_from(str, 0)
78   - if match
79   - Regexp.last_match = match
80   - return match.begin(0)
81   - else
82   - Regexp.last_match = nil
83   - return nil
84   - end
85   - end
86   -
87 70 def match_all(str)
88 71 start = 0
89 72 arr = []
17 kernel/common/regexp18.rb
@@ -42,6 +42,23 @@ def match(str)
42 42 Regexp.last_match = search_region(str, 0, str.bytesize, true)
43 43 end
44 44
  45 + # Returns the index of the first character in the region that
  46 + # matched or nil if there was no match. See #match for returning
  47 + # the MatchData instead.
  48 + def =~(str)
  49 + # unless str.nil? because it's nil and only nil, not false.
  50 + str = StringValue(str) unless str.nil?
  51 +
  52 + match = match_from(str, 0)
  53 + if match
  54 + Regexp.last_match = match
  55 + return match.begin(0)
  56 + else
  57 + Regexp.last_match = nil
  58 + return nil
  59 + end
  60 + end
  61 +
45 62 def ===(other)
46 63 unless other.kind_of?(String)
47 64 other = Rubinius::Type.check_convert_type other, String, :to_str
19 kernel/common/regexp19.rb
@@ -37,6 +37,7 @@ def match(str, pos=0)
37 37 return nil
38 38 end
39 39
  40 + str = str.to_s if str.is_a?(Symbol)
40 41 str = StringValue(str)
41 42 result = search_region(str, pos, str.bytesize, true)
42 43 Regexp.last_match = result
@@ -48,6 +49,24 @@ def match(str, pos=0)
48 49 end
49 50 end
50 51
  52 + # Returns the index of the first character in the region that
  53 + # matched or nil if there was no match. See #match for returning
  54 + # the MatchData instead.
  55 + def =~(str)
  56 + str = str.to_s if str.is_a?(Symbol)
  57 + # unless str.nil? because it's nil and only nil, not false.
  58 + str = StringValue(str) unless str.nil?
  59 +
  60 + match = match_from(str, 0)
  61 + if match
  62 + Regexp.last_match = match
  63 + return match.begin(0)
  64 + else
  65 + Regexp.last_match = nil
  66 + return nil
  67 + end
  68 + end
  69 +
51 70 def ===(other)
52 71 if other.kind_of? Symbol
53 72 other = other.to_s
10 spec/ruby/core/regexp/match_spec.rb
@@ -16,6 +16,12 @@
16 16 it "returns the index of the first character of the matching region" do
17 17 (/(.)(.)(.)/ =~ "abc").should == 0
18 18 end
  19 +
  20 + ruby_version_is "1.9" do
  21 + it "returns the index too, when argument is a Symbol" do
  22 + (/(.)(.)(.)/ =~ :abc).should == 0
  23 + end
  24 + end
19 25 end
20 26
21 27 describe "Regexp#match" do
@@ -26,6 +32,10 @@
26 32 end
27 33
28 34 ruby_version_is "1.9" do
  35 + it "returns a MatchData object, when argument is a Symbol" do
  36 + /(.)(.)(.)/.match(:abc).should be_kind_of(MatchData)
  37 + end
  38 +
29 39 it "matches the input at a given position" do
30 40 /./.match("abc", 1).begin(0).should == 1
31 41 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.