Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6352f2
commit c24ea20
Showing
8 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |