Skip to content

Instantly share code, notes, and snippets.

@burlesona
Created January 11, 2015 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burlesona/a0bd26437f91f72bf0fa to your computer and use it in GitHub Desktop.
Save burlesona/a0bd26437f91f72bf0fa to your computer and use it in GitHub Desktop.
Hash <-> Date
class Hash
def to_date
Date.new fetch(:year), fetch(:month), fetch(:day)
end
end
class Date
def to_hash
{year: year, month: month, day: day}
end
end
require 'minitest/autorun'
require 'minitest/spec'
describe "core extensions" do
describe "hash" do
it "should have to_date" do
res = {year: 2015, month: 12, day:25}.to_date
assert res.is_a?(Date)
assert_equal 2015, res.year
assert_equal 12, res.month
assert_equal 25, res.day
end
end
describe "date" do
it "should have to_hash" do
res = Date.new(2015,12,25).to_hash
assert res.is_a?(Hash)
assert_equal 2015, res[:year]
assert_equal 12, res[:month]
assert_equal 25, res[:day]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment