Created
October 25, 2008 02:09
-
-
Save Oshuma/19667 to your computer and use it in GitHub Desktop.
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
# Just in case you can't make up your mind. | |
# Provide a string of choices separated by 'or'. | |
# | |
# p = Picker.new('tits or gtfo or OMFG') | |
# p.choice # => 'tits' | |
# p.choice # => 'OMFG' | |
# | |
# | |
# The second argument can be a separator: | |
# | |
# p = Picker.new('tits || gtfo || OMFG', '||') | |
# p.choice # => 'tits' | |
# p.choice # => 'OMFG' | |
class Picker | |
def initialize(string, separator = 'or') | |
@separator = Regexp.escape(separator) | |
@choices = get_choices_from string | |
end | |
def choice | |
@choices.sort_by {rand}.first | |
end | |
private | |
def get_choices_from(string) | |
return [] unless string | |
string.split.grep(/[^#{@separator}]/i) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment