Class: ChRad::InputManager
- Defined in:
- lib/chrad/io_manager.rb
Instance Attribute Summary collapse
-
#force_stdin ⇒ Object
Returns the value of attribute force_stdin.
Attributes inherited from IOManager
#base, #digits, #mode, #separator, #stream
Instance Method Summary collapse
-
#initialize ⇒ InputManager
constructor
A new instance of InputManager.
- #name ⇒ Object
- #parse(str) ⇒ Object
Methods inherited from IOManager
#alphabet, #alphabet=, #automagic_find_alphabet, #setup_base_digits!, #value_to_digit
Constructor Details
#initialize ⇒ InputManager
Returns a new instance of InputManager.
71 72 73 74 75 |
# File 'lib/chrad/io_manager.rb', line 71 def initialize @base = DEFAULT_INPUT_BASE @stream = $stdin @force_stdin = false end |
Instance Attribute Details
#force_stdin ⇒ Object
Returns the value of attribute force_stdin.
69 70 71 |
# File 'lib/chrad/io_manager.rb', line 69 def force_stdin @force_stdin end |
Instance Method Details
#name ⇒ Object
77 78 79 |
# File 'lib/chrad/io_manager.rb', line 77 def name 'input' end |
#parse(str) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/chrad/io_manager.rb', line 81 def parse(str) case mode when :alphabet str.reverse.each_char.map.with_index do |c, idx| value = digits.index(c) if value.nil? raise Error, "Input digit #{c.inspect} is not a valid input digit (expected: #{digits.join('')})" end value * (base ** idx) end.reduce(&:+) when :list digits = str.split(separator).reverse digits.map.with_index do |x,idx| x.to_i * (base ** idx) end.reduce(&:+) else raise Error, "Invalid mode #{mode.inspect}" end end |