Skip to content

Commit

Permalink
add core extensions for Array and Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
phlipper committed Jul 13, 2012
1 parent 8326e78 commit aebd629
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/core_ext/array.rb
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
8 changes: 8 additions & 0 deletions lib/core_ext/hash.rb
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
3 changes: 3 additions & 0 deletions lib/pinboard_api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "faraday"
require "faraday_middleware"

require "core_ext/array"
require "core_ext/hash"

require "pinboard_api/post"
require "pinboard_api/tag"
require "pinboard_api/user"
Expand Down
11 changes: 11 additions & 0 deletions spec/core_ext/array_spec.rb
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
18 changes: 18 additions & 0 deletions spec/core_ext/hash_spec.rb
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

0 comments on commit aebd629

Please sign in to comment.