Class: Choice
- Inherits:
-
Object
- Object
- Choice
- Defined in:
- lib/fox/library/choice.rb
Instance Method Summary collapse
- #ask(question, type) ⇒ Object
- #get_choice_from_bipolar(question, allowed_answers = %w[ y n ]) ⇒ Object
- #get_choice_from_listing(question, data) ⇒ Object
- #get_choice_from_range(question, from = @percent_scale.first, to = @percent_scale.last) ⇒ Object
-
#initialize ⇒ Choice
constructor
A new instance of Choice.
Constructor Details
#initialize ⇒ Choice
Returns a new instance of Choice.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fox/library/choice.rb', line 12 def initialize # 0 = most dislike ; 5 = most like # This scale may have an statistical bias @likert_scale = [ "Strongly dislike", "Dislike", "Neither like nor dislike", "Like", "Strongly like" ] @phrase_completion_scale = [ "Definition of not xxxxx", "Strongly not xxxxx", "Stupid", "Not xxxxx at all", "Not so xxxxx", "Neutral", "A little bit xxxxx", "Rather xxxxx", "Really xxxxx", "Hilarious", "The most xxxxx ever", ] @percent_scale = [ 0, 100 ] end |
Instance Method Details
#ask(question, type) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/fox/library/choice.rb', line 126 def ask question, type allowed = [:bipolar, :likert, :phrase, :percent] answer = nil # Pre-condition check # {{{ raise ArgumentError, "The type (#{type.to_s}) is not among the allowed types (#{allowed.join(", ")})" unless( allowed.include?( type ) ) raise ArgumentError, "The question needs to be of type string but is (#{question.class.to_s})" unless( question.is_a?(String) ) # }}} # Main case type when :bipolar answer = get_choice_from_bipolar( question ) when :likert answer = get_choice_from_listing( question, @likert_scale ) when :phrase answer = get_choice_from_listing( question, @phrase_completion_scale ) when :percent answer = get_choice_from_range( question ) end # Post condition check answer end |
#get_choice_from_bipolar(question, allowed_answers = %w[ y n ]) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/fox/library/choice.rb', line 47 def get_choice_from_bipolar question, allowed_answers = %w[ y n ] print "#{question.to_s} [#{allowed_answers.join(", ")}] : " answer = STDIN.gets.to_s.chop return true if( answer =~ %r{y}i ) return false if( answer =~ %r{n}i ) end |
#get_choice_from_listing(question, data) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fox/library/choice.rb', line 61 def get_choice_from_listing question, data selection = nil while( selection.nil? ) print "#{question.to_s} : \n" data.each_with_index do |d, i| printf( "(%-2i) %s\n", i, d ) end print "\n>> " selection = ( ( STDIN.gets ).chomp ) end selection end |
#get_choice_from_range(question, from = @percent_scale.first, to = @percent_scale.last) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/fox/library/choice.rb', line 88 def get_choice_from_range question, from = @percent_scale.first, to = @percent_scale.last selection = nil finished = false while( not finished ) print "#{question.to_s} " printf( "(%i-%i) : ", from, to ) STDOUT.flush selection = ( ( STDIN.gets ).chomp ) if( selection == "" ) # puts "The selection needs to be of type integer!" finished = true else if( selection.tr( "0-9", "" ) == "" ) selection = selection.to_i if( selection >= from and selection <= to ) finished = true else puts "The selection needs to be inside the range from #{from.to_s} to #{to.to_s}!" end else puts "The input can only be of type integer" end end end # of while selection end |