Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5f015f56023b
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a3c648dca805
Choose a head ref
  • 4 commits
  • 2 files changed
  • 2 contributors

Commits on Dec 13, 2015

  1. [ruby-2.3 Feature #11785] add encoding: optional argument to `Strin…

    …g.new`
    
    Adds kwargs support to String#initialize such that it will accept an optional :encoding argument.
    cheald authored and kares committed Dec 13, 2015
    Copy the full SHA
    ce6f577 View commit details
  2. Move the initialize keywords out of the function so they aren't alloc…

    …ated per invocation. Slightly less typechecking; now behaves similarly to ruby-head when passed invalid types for arg0.
    cheald authored and kares committed Dec 13, 2015
    Copy the full SHA
    3e35382 View commit details
  3. validInitializeArgs -> static

    cheald authored and kares committed Dec 13, 2015
    Copy the full SHA
    cdfd906 View commit details
  4. "inline" kwargs handling for String.new encoding: ... + restore initi…

    …alize compatibility
    kares committed Dec 13, 2015
    3
    Copy the full SHA
    a3c648d View commit details
Showing with 51 additions and 7 deletions.
  1. +21 −4 core/src/main/java/org/jruby/RubyString.java
  2. +30 −3 test/mri/ruby/test_string.rb
25 changes: 21 additions & 4 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@
import org.joni.Region;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.platform.Platform;
import org.jruby.runtime.Block;
import org.jruby.runtime.ClassIndex;
@@ -1477,7 +1478,7 @@ public IRubyObject initialize(ThreadContext context) {
}

public IRubyObject initialize(ThreadContext context, IRubyObject arg0) {
return initialize19(context, arg0);
return initialize19(context, new IRubyObject[] { arg0 });
}

@JRubyMethod(name = "initialize", visibility = PRIVATE)
@@ -1486,9 +1487,25 @@ public IRubyObject initialize19(ThreadContext context) {
return this;
}

@JRubyMethod(name = "initialize", visibility = PRIVATE)
public IRubyObject initialize19(ThreadContext context, IRubyObject arg0) {
replace19(arg0);
@JRubyMethod(name = "initialize", visibility = PRIVATE, optional = 2)
public IRubyObject initialize19(ThreadContext context, IRubyObject[] args) {
if ( args.length == 0 ) return this;

IRubyObject arg = args[ args.length - 1 ];

final IRubyObject string; final IRubyObject encoding;
if ( arg instanceof RubyHash ) { // new encoding: ...
final RubySymbol enc = context.runtime.newSymbol("encoding");
encoding = ( (RubyHash) arg ).fastARef(enc);
string = args.length > 1 ? args[0] : null;
}
else {
string = arg; encoding = null;
}

if ( string != null ) replace19(string);
if ( encoding != null ) force_encoding(context, encoding);

return this;
}

33 changes: 30 additions & 3 deletions test/mri/ruby/test_string.rb
Original file line number Diff line number Diff line change
@@ -16,12 +16,39 @@ def initialize(*args)
super
end

def S(str)
@cls.new(str)
def S(*args)
@cls.new(*args)
end

def test_s_new
assert_equal("RUBY", S("RUBY"))
assert_equal("", S())
assert_equal(Encoding::ASCII_8BIT, S().encoding)

assert_equal("", S(""))
assert_equal(__ENCODING__, S("").encoding)

src = "RUBY"
assert_equal(src, S(src))
assert_equal(__ENCODING__, S(src).encoding)

src.force_encoding("euc-jp")
assert_equal(src, S(src))
assert_equal(Encoding::EUC_JP, S(src).encoding)


assert_equal("", S(encoding: "euc-jp"))
assert_equal(Encoding::EUC_JP, S(encoding: "euc-jp").encoding)

assert_equal("", S("", encoding: "euc-jp"))
assert_equal(Encoding::EUC_JP, S("", encoding: "euc-jp").encoding)

src = "RUBY"
assert_equal(src, S(src, encoding: "euc-jp"))
assert_equal(Encoding::EUC_JP, S(src, encoding: "euc-jp").encoding)

src.force_encoding("euc-jp")
assert_equal(src, S(src, encoding: "utf-8"))
assert_equal(Encoding::UTF_8, S(src, encoding: "utf-8").encoding)
end

def test_AREF # '[]'