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: 7c6f524a7ec0
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 527b036babc8
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 11, 2018

  1. Copy the full SHA
    b024241 View commit details
  2. Implement Hash#transform_keys and Hash#transform_keys!

    For more information, please see feature #13583.
    nomadium committed Jan 11, 2018
    Copy the full SHA
    5d431ce View commit details
  3. Merge pull request #4966 from nomadium/implement-hash-transform-keys

    Implement Hash#transform_keys and Hash#transform_keys!
    enebo authored Jan 11, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    527b036 View commit details
Showing with 48 additions and 0 deletions.
  1. +20 −0 core/src/main/java/org/jruby/RubyHash.java
  2. +28 −0 test/mri/ruby/test_hash.rb
20 changes: 20 additions & 0 deletions core/src/main/java/org/jruby/RubyHash.java
Original file line number Diff line number Diff line change
@@ -1430,11 +1430,31 @@ public IRubyObject each_key(final ThreadContext context, final Block block) {
return block.isGiven() ? each_keyCommon(context, block) : enumeratorizeWithSize(context, this, "each_key", enumSizeFn());
}

@JRubyMethod(name = "transform_keys")
public IRubyObject transform_keys(final ThreadContext context, final Block block) {
return ((RubyHash)dup()).transform_keys_bang(context, block);
}

@JRubyMethod(name = "transform_values")
public IRubyObject transform_values(final ThreadContext context, final Block block) {
return ((RubyHash)dup()).transform_values_bang(context, block);
}

@JRubyMethod(name = "transform_keys!")
public IRubyObject transform_keys_bang(final ThreadContext context, final Block block) {
if (block.isGiven()) {
testFrozen("Hash");
RubyHash bak = (RubyHash)dup();
rb_clear();
bak.keys().forEach((k) -> {
op_aset(context, block.yield(context, (IRubyObject)k), bak.op_aref(context, (IRubyObject)k));
});
return this;
}

return enumeratorizeWithSize(context, this, "transform_keys!", enumSizeFn());
}

@JRubyMethod(name = "transform_values!")
public IRubyObject transform_values_bang(final ThreadContext context, final Block block) {
if (block.isGiven()) {
28 changes: 28 additions & 0 deletions test/mri/ruby/test_hash.rb
Original file line number Diff line number Diff line change
@@ -1480,6 +1480,34 @@ def test_to_proc
assert_equal([10, 20, 30], [1, 2, 3].map(&h))
end

def test_transform_keys
x = @cls[a: 1, b: 2, c: 3]
y = x.transform_keys {|k| :"#{k}!" }
assert_equal({a: 1, b: 2, c: 3}, x)
assert_equal({a!: 1, b!: 2, c!: 3}, y)

enum = x.transform_keys
assert_equal(x.size, enum.size)
assert_instance_of(Enumerator, enum)

y = x.transform_keys.with_index {|k, i| "#{k}.#{i}" }
assert_equal(%w(a.0 b.1 c.2), y.keys)
end

def test_transform_keys_bang
x = @cls[a: 1, b: 2, c: 3]
y = x.transform_keys! {|k| :"#{k}!" }
assert_equal({a!: 1, b!: 2, c!: 3}, x)
assert_same(x, y)

enum = x.transform_keys!
assert_equal(x.size, enum.size)
assert_instance_of(Enumerator, enum)

x.transform_keys!.with_index {|k, i| "#{k}.#{i}" }
assert_equal(%w(a!.0 b!.1 c!.2), x.keys)
end

def test_transform_values
x = @cls[a: 1, b: 2, c: 3]
y = x.transform_values {|v| v ** 2 }