From 2329172f86b765522c5e6d586986c370a867222c Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 7 Dec 2019 10:17:12 -0800 Subject: [PATCH] [2019][ruby] simplify getting the parameter mode --- 2019/ruby/computer.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/2019/ruby/computer.rb b/2019/ruby/computer.rb index 911ad23..4111c41 100644 --- a/2019/ruby/computer.rb +++ b/2019/ruby/computer.rb @@ -95,11 +95,12 @@ class Computer @pc += 1 n = opcode.arity - 3 - args = (0...n).zip(T.must(instruction[0..2]).reverse.chars.map(&:to_i)).map {|i, mode| + args = (0...n).map {|i| + mode = instruction[2-i] { ?0 } value = @memory.fetch(@pc + i) || 0 mode = case mode - when 0 then Mode::Position - when 1 then Mode::Immediate + when ?0 then Mode::Position + when ?1 then Mode::Immediate else fail "unexpected mode: #{mode}" end Parameter.new(mode, value)