Last active
January 24, 2016 17:06
-
-
Save purp/fe713ca7bd0127aa377d to your computer and use it in GitHub Desktop.
Expand a two-element array by an interval
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 Array | |
def expand(interval=1) | |
raise "Can only expand two-element arrays" if self.size != 2 | |
raise "Can only expand numeric arrays" unless self.first.kind_of?(Numeric) && self.last.kind_of?(Numeric) | |
result = [self.first] | |
while result.last < self.last do | |
next_value = result.last + interval | |
if next_value < self.last | |
result << next_value | |
else | |
result << self.last | |
end | |
end | |
result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment