module GraderUtils

Public Class Methods

parse_array(input) click to toggle source
# File lib/grader-utils.rb, line 36
def self.parse_array(input)
  input = input.to_s.strip

  if input[0] == "[" && input[-1] == "]"
    input = input[1..-2]
  end

  input.split(',').map(&:strip)
end
parse_boolean(input) click to toggle source
# File lib/grader-utils.rb, line 12
def self.parse_boolean(input)
  input = parse_string(input).downcase
  if input == "true"
    true
  elsif input == "false"
    false
  else
    nil
  end
end
parse_number(input) click to toggle source
# File lib/grader-utils.rb, line 4
def self.parse_number(input)
  input = input.to_s.strip

  return nil unless input =~ /\A\-?\d*(?:\.\d*)?\z/

  input.to_f
end
parse_string(input) click to toggle source
# File lib/grader-utils.rb, line 23
def self.parse_string(input)
  input = input.to_s.strip

  if input[0] == "'" && input[-1] == "'"
    input[0] = input[-1] = '"'
    JSON.parse("[#{input}]")[0]
  elsif input[0] == '"' && input[-1] == '"'
    JSON.parse("[#{input}]")[0]
  else
    input
  end
end