Last active
August 29, 2015 14:03
-
-
Save rockarts/f2282b0ce22cc4f168f5 to your computer and use it in GitHub Desktop.
This will fail for any month except the current month but will get the amount of Tuesdays in the current month.
This file contains hidden or 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
| require 'minitest/autorun' | |
| require 'date' | |
| class TestWeekdayCounter < MiniTest::Unit::TestCase | |
| def test_number_of_tuesdays_in_june_2014_should_be_4 | |
| thisMonth = Date.new(2014, 6, 1) | |
| nextMonth = Date.new(2014, thisMonth.month + 1, 1) - 1 | |
| assert_equal 4, WeekdayCounter.new.get_tuesdays(thisMonth, nextMonth) | |
| end | |
| def test_number_of_tuesdays_in_july_2014_should_be_5 | |
| thisMonth = Date.new(2014, 7, 1) | |
| nextMonth = Date.new(2014, thisMonth.month + 1, 1) - 1 | |
| assert_equal 5, WeekdayCounter.new.get_tuesdays(thisMonth, nextMonth) | |
| end | |
| end | |
| class WeekdayCounter | |
| def get_tuesdays(start_date, end_date) | |
| tuesdays = 0 | |
| (start_date..end_date).each do |date| | |
| if date.wday.eql? 2 | |
| tuesdays = tuesdays + 1 | |
| end | |
| end | |
| tuesdays | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment