Created
August 22, 2013 20:06
-
-
Save samullen/6312118 to your computer and use it in GitHub Desktop.
Wanting opinions on the each method. Am I doing this right?
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
| class Timeframe | |
| include Enumerable | |
| attr_accessor :start_time, :end_time | |
| ONEDAY = 24 * 60 * 60 | |
| def initialize(start_time, end_time) | |
| @start_time = start_time | |
| @end_time = end_time | |
| end | |
| def each | |
| @current_time = @start_time | |
| while @current_time <= @end_time | |
| yield @current_time | |
| @current_time += ONEDAY | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@johnkary I'm just in the lazy habit of using
attr_accessor. I'm a horrible person.@KellyMahan The
#eachmethod is a special method used by theEnumerablemodule. When you defineeachin a class in whichEnumerablehas been included, you get instant access to most ofEnumerable's methods (in this case, I wantedmap).But you're right,
each_daywould be much better, and so I think changing the name of the class might makeeachmake a little more sense. I may change it toDaterangeorDayrange. Not sure.Really appreciate the feedback.