Skip to content

Commit

Permalink
Remove special case logic for String#slice(<Symbol>)
Browse files Browse the repository at this point in the history
It is okay to remove this special-case logic here
because Symbol#to_int no longer exists in Ruby 1.9+,
so Symbol-to-Fixnum coercion will fail with TypeError
(which matches MRI behavior).
Resolves #3335.
jemc committed Mar 2, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8d7cba8 commit b6b0a45
Showing 4 changed files with 14 additions and 5 deletions.
5 changes: 0 additions & 5 deletions kernel/common/string.rb
Original file line number Diff line number Diff line change
@@ -152,11 +152,6 @@ def [](index, other = undefined)
length = 0 if length < 0

return substring(start, length)
# A really stupid case hit for rails. Either we define this or we define
# Symbol#to_int. We removed Symbol#to_int in late 2007 because it's evil,
# and do not want to re add it.
when Symbol
return nil
else
index = Rubinius::Type.coerce_to index, Fixnum, :to_int
return self[index]
4 changes: 4 additions & 0 deletions spec/ruby/core/string/element_reference_spec.rb
Original file line number Diff line number Diff line change
@@ -29,3 +29,7 @@
describe "String#[] with String" do
it_behaves_like :string_slice_string, :[]
end

describe "String#[] with String" do
it_behaves_like :string_slice_symbol, :[]
end
6 changes: 6 additions & 0 deletions spec/ruby/core/string/shared/slice.rb
Original file line number Diff line number Diff line change
@@ -525,3 +525,9 @@
end
end
end

describe :string_slice_symbol, :shared => true do
it "raises TypeError" do
lambda { 'hello'.send(@method, :hello) }.should raise_error(TypeError)
end
end
4 changes: 4 additions & 0 deletions spec/ruby/core/string/slice_spec.rb
Original file line number Diff line number Diff line change
@@ -32,6 +32,10 @@
it_behaves_like :string_slice_string, :slice
end

describe "String#slice with Symbol" do
it_behaves_like :string_slice_symbol, :slice
end

describe "String#slice! with index" do
it "deletes and return the char at the given position" do
a = "hello"

0 comments on commit b6b0a45

Please sign in to comment.