-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add core extensions for Array and Hash
- Loading branch information
Showing
5 changed files
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Array | ||
def self.wrap(object) | ||
if object.nil? | ||
[] | ||
elsif object.respond_to?(:to_ary) | ||
object.to_ary || [object] | ||
else | ||
[object] | ||
end | ||
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,8 @@ | ||
class Hash | ||
def stringify_keys! | ||
keys.each do |key| | ||
self[key.to_s] = delete(key) | ||
end | ||
self | ||
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 @@ | ||
require "spec_helper" | ||
|
||
describe Array do | ||
|
||
describe "self.wrap" do | ||
it { Array.wrap(nil).must_equal [] } | ||
it { Array.wrap("string").must_equal ["string"] } | ||
it { Array.wrap(["array"]).must_equal ["array"] } | ||
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 @@ | ||
require "spec_helper" | ||
|
||
describe Hash do | ||
|
||
describe "stringify_keys!" do | ||
let(:string_hash) do | ||
{ "foo" => "bar", "baz" => "qux" } | ||
end | ||
|
||
let(:symbol_hash) do | ||
{ foo: "bar", baz: "qux" } | ||
end | ||
|
||
it { symbol_hash.stringify_keys!.must_equal string_hash } | ||
it { string_hash.stringify_keys!.must_equal string_hash } | ||
end | ||
|
||
end |