Class: Roebe::Quiz

Inherits:
Base show all
Defined in:
lib/roebe/classes/quiz.rb

Overview

Roebe::Quiz

Constant Summary collapse

DEFAULT_SLEEP_TIME =
#

DEFAULT_SLEEP_TIME

#
2.5
EDITOR =
#

EDITOR

#
ENV['EDITOR'].to_s
SCIENCE_DIRECTORY =
HOME_DIRECTORY_OF_USER_X+'DATA/science/'
FILE_WORLD_CAPITAL_CITIES =
#

FILE_WORLD_CAPITAL_CITIES

#
(
  File.absolute_path(
    Roebe.project_base_dir?+
    'yaml/world_capitals/world_capitals.yml'
  )
).squeeze '/'
ALL_CAPITALS =
#

ALL_CAPITALS

#
[]
ALL_CAPITALS_REVERSED =
ALL_CAPITALS.invert
ARRAY_SKIP_THESE =
#

ARRAY_SKIP_THESE

#
[ '[]','[DONE]','DONE' ]

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N, Base::NAMESPACE

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #show_help, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(run_already = true) ⇒ Quiz

#

initialize

#


69
70
71
72
73
74
75
76
77
# File 'lib/roebe/classes/quiz.rb', line 69

def initialize(
    run_already = true
  )
  reset
  if run_already == :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Instance Method Details

#ask_the_questionObject

#

ask_the_question

#


200
201
202
203
204
205
206
207
# File 'lib/roebe/classes/quiz.rb', line 200

def ask_the_question # This will ask the question.
  fancy N+'Die Hauptstadt von ',false
  e
  emphasis '  '+@random_key,false
  e
  fancy 'ist?'
  @n_questions_asked += 1 # Also count up since Oct 2011.
end

#emphasis(i, make_newline = true) ⇒ Object

#

emphasis

#


232
233
234
235
236
# File 'lib/roebe/classes/quiz.rb', line 232

def emphasis(i, make_newline = true)
  _ = @colour_emphasis+i
  _ << N if make_newline
  e _
end

#enter_threadObject

#

enter_thread

#


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/roebe/classes/quiz.rb', line 141

def enter_thread
  # after ~3 seconds we display even more
  @thread = Thread.new {
    sleep(@sleep_for_how_long)
    if @reverse_question_answer
      say_the_answer(ALL_CAPITALS_REVERSED[@random_key])
    else
      say_the_answer
    end
  }
  begin
    steal_focus
    @thread.join
  rescue Interrupt # rescue interrupt and exit.
    # system 'stty echo'
    restore_terminal
    exit
  end
end

#fancy(i, make_newline = true) ⇒ Object

#

fancy

#


212
213
214
215
216
217
218
# File 'lib/roebe/classes/quiz.rb', line 212

def fancy(
    i, make_newline = true
  )
  _ = @colour_fancy+i
  _ << N if make_newline
  e _
end

#find_capitalObject

#

find_capital

#


98
99
100
101
102
103
104
105
# File 'lib/roebe/classes/quiz.rb', line 98

def find_capital
  if @reverse_question_answer
    @keys = ALL_CAPITALS_REVERSED.keys
  else
    @keys = ALL_CAPITALS.keys
  end
  @random_key = @keys.sample
end

#important(i, make_newline = true) ⇒ Object

#

important

#


223
224
225
226
227
# File 'lib/roebe/classes/quiz.rb', line 223

def important(i, make_newline = true)
  _ = @colour_important+i
  _ << N if make_newline
  e _
end

#parse_this_commandline(i) ⇒ Object

#

parse_this_commandline

#


250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/roebe/classes/quiz.rb', line 250

def parse_this_commandline(i)
  if i.is_a? Array
    case i.first
    when 'file?'
      show_yaml_file
      exit
    when 'HELP'
      show_help_options
      exit
    end
  end
end

#resetObject

#

reset

#


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/roebe/classes/quiz.rb', line 82

def reset
  super()
  infer_the_namespace
  self.set_sleep_for_how_long = DEFAULT_SLEEP_TIME
  # ======================================================================= #
  # === @run_that_many_questions
  # ======================================================================= #
  @run_that_many_questions = nil
  @n_questions_asked = 0
  @reverse_question_answer = false # if true then we ask vice versa.
  set_colours
end

#restore_terminalObject

#

restore_terminal

#


241
242
243
244
245
# File 'lib/roebe/classes/quiz.rb', line 241

def restore_terminal
  e rev+N
  system 'stty -raw echo'
  e
end

#runObject

#

run

#


283
284
285
286
287
288
# File 'lib/roebe/classes/quiz.rb', line 283

def run # The inner logic of the class.
  find_capital
  ask_the_question
  enter_thread
  restore_terminal
end

#run_in_a_loopObject

#

run_in_a_loop (loop tag)

#


121
122
123
124
125
126
127
128
129
# File 'lib/roebe/classes/quiz.rb', line 121

def run_in_a_loop
  loop {
    run
    if @run_that_many_questions == @n_questions_asked
      system 'stty echo'
      break
    end
  } # We wrap the main logic into a loop here.
end

#run_that_many_questions(i) ⇒ Object

#

run_that_many_questions

#


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/roebe/classes/quiz.rb', line 171

def run_that_many_questions(i)
  case i # case tag
  # ======================================================================== #
  # === quiz --open
  # ======================================================================== #
  when 'OPEN',/-?-?open/
    esystem EDITOR+' '+__FILE__
    exit
  # ======================================================================== #
  # === quiz --revert
  # ======================================================================== #
  when /-?-?revert/,'rev','reversed'
    @reverse_question_answer = true
    fancy 'We will from now on ask for the land of '+
          'the given capital.'+N
  end
  @run_that_many_questions = i.to_i
end

#say_the_answer(hash = ) ⇒ Object

#

say_the_answer

#


193
194
195
# File 'lib/roebe/classes/quiz.rb', line 193

def say_the_answer(hash = ALL_CAPITALS[@random_key]) # say the proper answer here.
  important N+'               '+hash # Since Oct 2011 we pad it a little.
end

#set_coloursObject

#

set_colours

Which colours we will use internally to display things.

#


112
113
114
115
116
# File 'lib/roebe/classes/quiz.rb', line 112

def set_colours
  @colour_fancy     = ::Colours::GREEN
  @colour_important = ::Colours::RED
  @colour_emphasis  = ::Colours::YEL
end

#set_sleep_for_how_long=(i) ⇒ Object Also known as: set_delay

#

set_sleep_for_how_long=

#


134
135
136
# File 'lib/roebe/classes/quiz.rb', line 134

def set_sleep_for_how_long=(i)
  @sleep_for_how_long = i.to_i
end

#show_help_optionsObject

#

show_help_options (help tag)

#


266
267
268
269
270
271
# File 'lib/roebe/classes/quiz.rb', line 266

def show_help_options
  restore_terminal
  opn; ecomment '  ALL_CAPITALS # Show all available capitals'
  opn; ecomment '  file?        # Show the yaml file'
  opn; e 'Also note that we will skip entries with []'
end

#show_yaml_fileObject

#

show_yaml_file

#


276
277
278
# File 'lib/roebe/classes/quiz.rb', line 276

def show_yaml_file
  opn; e WORLD_CAPITAL_CITIES
end

#steal_focusObject

#

steal_focus

#


164
165
166
# File 'lib/roebe/classes/quiz.rb', line 164

def steal_focus
  system 'stty raw -echo'
end