class Judges::Options

License
MIT
Copyright
Copyright © 2024-2025 Yegor Bugayenko
Author

Yegor Bugayenko (yegor256@gmail.com)
allowing options to be accessed as method calls.
The class also provides dynamic method access to options using the ‘others’ gem,
to appropriate types (integers for numeric strings, etc.).
normalized into a hash with symbol keys. Values are automatically converted
Options are key-value pairs that can be provided in various formats and are
This class manages configuration options that can be passed to judge scripts.
Options for Ruby scripts in the judges.

def +(other)

Other tags:
    Example: Merge two Options objects -

Returns:
  • (Judges::Options) - A new Options object with merged values

Parameters:
  • other (Judges::Options) -- The other options to merge
def +(other)
  h = to_h
  other.to_h.each do |k, v|
    h[k] = v
  end
  Judges::Options.new(h)
end

def empty?

Other tags:
    Example: Check if options are empty -

Returns:
  • (Boolean) - true if no options are set, false otherwise
def empty?
  to_h.empty?
end

def initialize(pairs = nil)

Other tags:
    Example: Initialize with hash -
    Example: Initialize with string -
    Example: Initialize with array -

Parameters:
  • pairs (Array, String, Hash, nil) -- List of key-value pairs.
def initialize(pairs = nil)
  @pairs = pairs
end

def to_h

Other tags:
    Example: Convert to hash -

Returns:
  • (Hash) - The options as a hash with symbol keys
def to_h
  @to_h ||=
    begin
      pp = @pairs || []
      pp = pp.split(',') if pp.is_a?(String)
      if pp.is_a?(Array)
        pp = pp
          .compact
          .map(&:strip)
          .reject(&:empty?)
          .map { |s| s.split('=', 2) }
          .map { |a| a.size == 1 ? [a[0], nil] : a }
          .reject { |a| a[0].empty? }
          .to_h
      end
      pp
        .reject { |k, _| k.nil? }
        .reject { |k, _| k.is_a?(String) && k.empty? }
        .to_h
        .transform_values { |v| v.nil? ? 'true' : v }
        .transform_values { |v| v.is_a?(String) ? v.strip : v }
        .transform_values { |v| v.is_a?(String) && v.match?(/^[0-9]+$/) ? v.to_i : v }
        .transform_keys { |k| k.to_s.strip.upcase.to_sym }
    end
end

def to_s

Other tags:
    Example: Convert to string -

Returns:
  • (String) - Formatted string with each option on a new line
def to_s
  to_h.map do |k, v|
    v = v.to_s
    v = "#{v[0..3]}#{'*' * (v.length - 8)}#{v[-4..]}" if v.length > 8
    "#{k}\"#{v}\""
  end.sort.join("\n")
end