Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created October 25, 2008 02:09
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 Oshuma/19667 to your computer and use it in GitHub Desktop.
Save Oshuma/19667 to your computer and use it in GitHub Desktop.
# 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