Last active
November 22, 2015 18:38
-
-
Save YanTheFawn/4a7ac319c4bc9cb59b42 to your computer and use it in GitHub Desktop.
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 Range | |
def oscillate(num_times: 1) | |
previous_value = nil | |
num_times.times do |index| | |
first.upto(last) do |value| | |
#protect against repeat values, since | |
#each oscillation directions don't know | |
#about the other, and will otherwise | |
#redundantly output the limit values | |
yield value unless (value === previous_value) | |
previous_value = value | |
end | |
last.downto(first) do |value| | |
yield value unless (value === previous_value) | |
previous_value = value | |
end | |
end | |
end | |
end | |
(1..5).oscillate(num_times: 2) {|val| puts "val is #{val}"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment