diff --git a/core/hash.rb b/core/hash.rb index f3bd0491c1..38de80e48f 100644 --- a/core/hash.rb +++ b/core/hash.rb @@ -166,12 +166,13 @@ def delete(key) var map = #{self}.map, result; if (result = map[key]) { - result = bucket[1]; + result = result[1]; delete map[key]; + return result; } - return result; + return nil; } end diff --git a/spec/core/hash/constructor_spec.rb b/spec/core/hash/constructor_spec.rb new file mode 100644 index 0000000000..b28c6a5e97 --- /dev/null +++ b/spec/core/hash/constructor_spec.rb @@ -0,0 +1,13 @@ +describe "Hash.[]" do + describe "passed zero arguments" do + it "returns an empty hash" do + Hash[].should == {} + end + end + + it "creates a Hash; values can be provided as the argument list" do + Hash[:a, 1, :b, 2].should == { :a => 1, :b => 2 } + Hash[].should == {} + Hash[:a, 1, :b, {:c => 2}].should == {:a => 1, :b => {:c => 2}} + end +end \ No newline at end of file diff --git a/spec/core/hash/default_proc_spec.rb b/spec/core/hash/default_proc_spec.rb new file mode 100644 index 0000000000..e50adba60b --- /dev/null +++ b/spec/core/hash/default_proc_spec.rb @@ -0,0 +1,20 @@ +describe "Hash#default_proc" do + it "returns the block passed to Hash.new" do + h = Hash.new { |i| 'Paris' } + p = h.default_proc + p.call(1).should == 'Paris' + end + + it "returns nil if no block was passed to proc" do + {}.default_proc.should == nil + end +end + +describe "Hash#default_proc=" do + it "replaces the block passed to Hash.new" do + h = Hash.new { |i| 'Paris' } + h.default_proc = Proc.new { 'Montreal' } + p = h.default_proc + p.call(1).should == 'Montreal' + end +end \ No newline at end of file diff --git a/spec/core/hash/default_spec.rb b/spec/core/hash/default_spec.rb index 8819addf68..c7dfc74124 100644 --- a/spec/core/hash/default_spec.rb +++ b/spec/core/hash/default_spec.rb @@ -6,4 +6,12 @@ Hash.new.default.should == nil Hash.new.default(4).should == nil end +end + +describe "Hash#default=" do + it "sets the default value" do + h = {} + h.default = 99 + h.default.should == 99 + end end \ No newline at end of file diff --git a/spec/core/hash/delete_spec.rb b/spec/core/hash/delete_spec.rb new file mode 100644 index 0000000000..3d06637a98 --- /dev/null +++ b/spec/core/hash/delete_spec.rb @@ -0,0 +1,11 @@ +describe "Hash#delete" do + it "removes the entry and returns the deleted value" do + h = {a: 5, b: 2} + h.delete(:b).should == 2 + h.should == {a: 5} + end + + it "returns nil if the key is not found" do + {}.delete(:a).should == nil + end +end \ No newline at end of file diff --git a/spec/core/hash/dup_spec.rb b/spec/core/hash/dup_spec.rb new file mode 100644 index 0000000000..9494f7e568 --- /dev/null +++ b/spec/core/hash/dup_spec.rb @@ -0,0 +1,10 @@ +describe "Hash#dup" do + it "copies instance variable but not the objects they refer to" do + hash = {'key' => 'value'} + + clone = hash.dup + + clone.should == hash + clone.object_id.should_not == hash.object_id + end +end \ No newline at end of file diff --git a/spec/core/hash/reject_spec.rb b/spec/core/hash/reject_spec.rb new file mode 100644 index 0000000000..37c3be3363 --- /dev/null +++ b/spec/core/hash/reject_spec.rb @@ -0,0 +1,18 @@ +describe "Hash#reject" do + it "returns a new hash removing keys for which the block yields true" do + h = {1=>false, 2=>true, 3=>false, 4=>true} + h.reject { |k,v| v }.keys.should == [1,3] + end + + it "is equivalent to hsh.dup.delete_if" do + h = {:a => 'a', :b => 'b', :c => 'd'} + h.reject { |k,v| k == 'd' }.should == (h.dup.delete_if { |k, v| k == 'd' }) + + all_args_reject = [] + all_args_delete_if = [] + h = {1 => 2, 3 => 4} + h.reject { |*args| all_args_reject << args } + h.delete_if { |*args| all_args_delete_if << args } + all_args_reject.should == all_args_delete_if + end +end \ No newline at end of file diff --git a/spec/core/hash/to_a_spec.rb b/spec/core/hash/to_a_spec.rb new file mode 100644 index 0000000000..de4a53ba32 --- /dev/null +++ b/spec/core/hash/to_a_spec.rb @@ -0,0 +1,13 @@ +describe "Hash#to_a" do + it "returns a list of [key, value] pairs with same order as each()" do + h = {:a => 1, 1 => :a, 3 => :b, :b => 5} + pairs = [] + + h.each_pair do |key, value| + pairs << [key, value] + end + + h.to_a.should be_kind_of(Array) + h.to_a.should == pairs + end +end \ No newline at end of file