Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Regexp new with numbers #2960

Closed
wants to merge 2 commits into from

2 participants

@jc00ke
Owner

Adding on to the if/elsif chain feels dirty. Suggestions, or leave as is?

@dbussink
Owner

Shouldn't this implement a coercion protocol like we have in place in a lot of other places?

@jc00ke
Owner
@dbussink
Owner

I suspect this should be string coercion, but not sure. We should also check behavior like calling to_s / to_str in the argument.

@jc00ke jc00ke closed this
@jc00ke jc00ke deleted the regexp-new-with-numbers branch
@jc00ke
Owner

Will reopen when it's actually a decent PR :wink:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 2, 2014
  1. @jc00ke
  2. @jc00ke
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 0 deletions.
  1. +2 −0  kernel/common/regexp.rb
  2. +13 −0 spec/ruby/core/regexp/new_spec.rb
View
2  kernel/common/regexp.rb
@@ -128,6 +128,8 @@ def initialize(pattern, opts=nil, lang=nil)
if pattern.kind_of?(Regexp)
opts = pattern.options
pattern = pattern.source
+ elsif pattern.kind_of?(Fixnum) or pattern.kind_of?(Float)
+ raise TypeError, "can't convert Fixnum into String"
elsif opts.kind_of?(Fixnum)
opts = opts & (OPTION_MASK | KCODE_MASK) if opts > 0
elsif opts
View
13 spec/ruby/core/regexp/new_spec.rb
@@ -12,3 +12,16 @@
describe "Regexp.new given a Regexp" do
it_behaves_like :regexp_new_regexp, :new
end
+
+describe "Regexp.new given a Fixnum" do
+ it "raises a TypeError" do
+ lambda { Regexp.new(1) }.should raise_error(TypeError)
+ end
+end
+
+describe "Regexp.new given a Float" do
+ it "raises a TypeError" do
+ lambda { Regexp.new(1.0) }.should raise_error(TypeError)
+ end
+end
+
Something went wrong with that request. Please try again.