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: 623b53771e33
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 93bd82f2e134
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Aug 12, 2016

  1. 1
    Copy the full SHA
    802f13e View commit details
  2. Moved to ruby/spec.

    headius committed Aug 12, 2016
    Copy the full SHA
    d7f0393 View commit details
  3. Copy the full SHA
    ae7e964 View commit details
  4. Copy the full SHA
    93bd82f View commit details
3 changes: 1 addition & 2 deletions core/src/main/ruby/jruby/kernel/hash.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Hash
def to_proc
hash_self = self
proc {|key| hash_self[key]}
method(:[]).to_proc
end
end
34 changes: 27 additions & 7 deletions spec/ruby/core/hash/to_proc_spec.rb
Original file line number Diff line number Diff line change
@@ -20,21 +20,41 @@
@proc = @hash.to_proc
end

it "is not a lambda" do
@proc.lambda?.should == false
end

it "raises ArgumentError if not passed exactly one argument" do
lambda {
@proc.call
}.should raise_error(ArgumentError)

lambda {
@proc.call 1, 2
}.should raise_error(ArgumentError)
end

context "with a stored key" do
it "returns the paired value" do
@proc.call(@key).should equal(@value)
end
end

context "passed as the block for instance_exec" do
it "always retrieves the original hash's values" do
hash = {foo: 1, bar: 2}
proc = hash.to_proc
context "passed as a block" do
it "retrieves the hash's values" do
[@key].map(&@proc)[0].should equal(@value)
end

context "to instance_exec" do
it "always retrieves the original hash's values" do
hash = {foo: 1, bar: 2}
proc = hash.to_proc

hash.instance_exec(:foo, &proc).should == 1
hash.instance_exec(:foo, &proc).should == 1

hash2 = {quux: 1}
hash2.instance_exec(:foo, &proc).should == 1
hash2 = {quux: 1}
hash2.instance_exec(:foo, &proc).should == 1
end
end
end

1 change: 1 addition & 0 deletions spec/tags/ruby/core/hash/to_proc_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fails:Hash#to_proc the returned proc is not a lambda
2 changes: 2 additions & 0 deletions spec/truffle/tags/core/hash/to_proc_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fails:Hash#to_proc the returned proc is not a lambda
fails:Hash#to_proc the returned proc passed as a block to instance_exec always retrieves the original hash's values
15 changes: 0 additions & 15 deletions test/jruby/test_hash.rb
Original file line number Diff line number Diff line change
@@ -108,19 +108,4 @@ def test_compare_by_identity
assert_equal 2, hash[arr2]
end

def test_to_proc
# TODO move to RubySpec
h = { 1 => 10, false => true, '2' => :'20' }
assert_equal false, h.to_proc.lambda?
proc = h.to_proc
assert_equal 10, proc.call(1)
assert_equal true, proc.call(false)
assert_equal nil, proc.call(2)

assert_raises(ArgumentError) { proc.call }
assert_raises(ArgumentError) { proc.call 1, 2 }

assert_equal [ 10, 20.to_s.to_sym ], [ 1, '2' ].map(&h)
end

end