Skip to content

Instantly share code, notes, and snippets.

@theinventor
Created June 17, 2014 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theinventor/654a3902f100fb7d52db to your computer and use it in GitHub Desktop.
Save theinventor/654a3902f100fb7d52db to your computer and use it in GitHub Desktop.
Round to nearest 5 minutes in ruby, strangely this wasn't super simple to come up with..
def self.round_to_five_minutes(minute)
all = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
higher = nil
lower = nil
all.each_with_index do |five_min,index|
if minute == five_min
return five_min
end
if five_min > minute
higher = five_min
lower = all[index-1]
break
end
end
diff_lower = minute-lower
diff_higher = higher-minute
diff_lower > diff_higher ? higher : lower
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment