Created
June 28, 2016 14:13
-
-
Save tomaszgiba/ace820a36f4e5ce0665867abccd2afbe to your computer and use it in GitHub Desktop.
Return array element basing on a ANY value and have deterministic output.
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
def arr_element_from_value(value, array) | |
array[(value.hash % (array.count).abs) + [0, array.count].min] | |
end | |
arr = ['#FFFFFF', '#666666', '#D8D8D8', '#333333', '#378c89'] | |
arr_element_from_value(nil, arr) # ¯\_(ツ)_/¯ | |
arr_element_from_value(1, arr) | |
arr_element_from_value(SecureRandom.hex, arr) | |
arr_element_from_value(1.0, arr) | |
arr_element_from_value(User.last, arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment