Skip to content

Commit

Permalink
Make it clear that SB#[]= takes 3 arguments, and reduce Array allocation
Browse files Browse the repository at this point in the history
this reduces a redundant Array allocation that used to be created by *.
Plus, added some tests for []= with three arguments.
  • Loading branch information
amatsuda committed Jan 5, 2023
1 parent d247f49 commit c9875d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Expand Up @@ -88,11 +88,11 @@ def replace(value)
super(implicit_html_escape_interpolated_argument(value))
end

def []=(*args)
if args.length == 3
super(args[0], args[1], implicit_html_escape_interpolated_argument(args[2]))
def []=(arg1, arg2, arg3 = nil)
if arg3
super(arg1, arg2, implicit_html_escape_interpolated_argument(arg3))
else
super(args[0], implicit_html_escape_interpolated_argument(args[1]))
super(arg1, implicit_html_escape_interpolated_argument(arg2))
end
end

Expand Down
12 changes: 12 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -1011,6 +1011,12 @@ def to_s

assert_equal "<b>oo", string
assert_predicate string, :html_safe?

string = "foo".html_safe
string[0, 2] = "<b>".html_safe

assert_equal "<b>o", string
assert_predicate string, :html_safe?
end

test "Replacing index of safe with unsafe yields escaped safe" do
Expand All @@ -1019,6 +1025,12 @@ def to_s

assert_equal "&lt;b&gt;oo", string
assert_predicate string, :html_safe?

string = "foo".html_safe
string[1, 1] = "<b>"

assert_equal "f&lt;b&gt;o", string
assert_predicate string, :html_safe?
end

test "emits normal string YAML" do
Expand Down

0 comments on commit c9875d3

Please sign in to comment.