Skip to content

Instantly share code, notes, and snippets.

@sentientmonkey
Created June 26, 2014 00:25
Show Gist options
  • Select an option

  • Save sentientmonkey/848e841d247c8faff4b1 to your computer and use it in GitHub Desktop.

Select an option

Save sentientmonkey/848e841d247c8faff4b1 to your computer and use it in GitHub Desktop.
require "minitest/autorun"
require "date"
class Person
CURRENT_YEAR = ->{ Date.new.year }
attr_reader :birthday
def initialize(attributes={})
@birthday = attributes.fetch(:birthday)
end
def can_drink?(year_function = CURRENT_YEAR)
age(year_function) > 21
end
def age(year_function = CURRENT_YEAR)
year_function.call - birthday.year
end
end
describe Person do
let(:seventy_niner) do
Person.new birthday: Date.new(1979)
end
it "age is determined by their birthday" do
seventy_niner.age(->{ 2009 }).must_equal 30
end
it "can drink if older than 21" do
seventy_niner.can_drink?(->{ 2009 }).must_equal true
end
it "CURRENT_YEAR is today" do
Person::CURRENT_YEAR.call.must_equal Date.new.year
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment