Class: DataStructureQuestion

Inherits:
Question show all
Defined in:
lib/battleroom/models/data_structure_question.rb

Instance Attribute Summary collapse

Attributes inherited from Question

#answer_value, #data, #data_structure_class, #evaluation_scope, #explanation, #input_mechanism, #user_input, #variable_name, #variable_value

Instance Method Summary collapse

Methods inherited from Question

#congratulation_sequence, #enter_evaluation_loop, generate_question, #get_input, #handle_syntax_error_exceptions

Constructor Details

#initialize(eval_scope) ⇒ DataStructureQuestion

Returns a new instance of DataStructureQuestion.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/battleroom/models/data_structure_question.rb', line 6

def initialize(eval_scope)
  super(eval_scope)
  @data_structure = data[:data_structure].clone
  if data_structure.class == Array
    # randomizes and shuffles the items in the arrays, so repeats remain interesting
    self.data_structure = data_structure.shuffle
    self.hint = 'index values start at 0.'
  else
    convert_keys_to_strings if [0,1,2,3,4].sample.odd?
    self.hint = 'you have to use the EXACT hash key to retrieve the associated value.'
  end
  self.possible_assignments = []
end

Instance Attribute Details

#data_structureObject

Returns the value of attribute data_structure.



4
5
6
# File 'lib/battleroom/models/data_structure_question.rb', line 4

def data_structure
  @data_structure
end

#hintObject

Returns the value of attribute hint.



4
5
6
# File 'lib/battleroom/models/data_structure_question.rb', line 4

def hint
  @hint
end

#possible_assignmentsObject

Returns the value of attribute possible_assignments.



4
5
6
# File 'lib/battleroom/models/data_structure_question.rb', line 4

def possible_assignments
  @possible_assignments
end

Instance Method Details

#convert_keys_to_stringsObject



20
21
22
23
24
25
26
27
# File 'lib/battleroom/models/data_structure_question.rb', line 20

def convert_keys_to_strings
  altered_ds = data_structure.each_with_object({}) do |key_value_array, new_hash|
    old_key = key_value_array[0]
    value = key_value_array[1]
    new_hash[old_key.to_s] = value
  end
  self.data_structure = altered_ds
end

#cull_hash_to_valid_size_for_outputObject



29
30
31
32
33
34
35
36
37
# File 'lib/battleroom/models/data_structure_question.rb', line 29

def cull_hash_to_valid_size_for_output
  desired_hash_size = rand(2..4)
  while data_structure.size > desired_hash_size
    key_to_delete = data_structure.keys.sample
    value_deleted = data_structure.delete(key_to_delete)
    new_assignment_possibility = { key_to_delete => value_deleted }
    possible_assignments.push(new_assignment_possibility)
  end
end

#find_number_of_boolean_values_in_hashObject



39
40
41
# File 'lib/battleroom/models/data_structure_question.rb', line 39

def find_number_of_boolean_values_in_hash
  data_structure.select { |key, value| value.to_s.match(/true|false/i) }.size
end