Skip to content

Instantly share code, notes, and snippets.

@YanTheFawn
Last active November 22, 2015 18:38
Show Gist options
  • Save YanTheFawn/4a7ac319c4bc9cb59b42 to your computer and use it in GitHub Desktop.
Save YanTheFawn/4a7ac319c4bc9cb59b42 to your computer and use it in GitHub Desktop.
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