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 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 |
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
#each
method is a special method used by theEnumerable
module. When you defineeach
in a class in whichEnumerable
has been included, you get instant access to most ofEnumerable
's methods (in this case, I wantedmap
).But you're right,
each_day
would be much better, and so I think changing the name of the class might makeeach
make a little more sense. I may change it toDaterange
orDayrange
. Not sure.Really appreciate the feedback.