Ior_code_revisited

Ior wants to change parameters. He has a hash (or dictionary) with the parameters that needs to change and what they need to change into.

This is what he comes up with:

ARGS = ['--number', '8', '--verbose', 'file.dat']

change = {'--number': '-n', '--verbose': '-v'}


argz = []
ARGS.each {|arg|
  found = false
  change.each {|key, val|
    if arg == "#{key}"
      argz.append(val)
      found = true
      break
    end
  }
  argz.append(arg) unless found
}

p argz

Why? The problem is that Ior programs like he think:

“First I do this.. then I must do that. But if that or this occurs.. I must instead do this before that and do the other after that.”

So he repeatedly finds himself painted into corners.. or in the need to program himself out of a mess.

You can recognize Ior-code by nested loops; a lot of type conversions; temporary variables; if-if-if-else-except.

If he instead just focused on what needs to be done.. this would be the result:

ARGS = ['--number', '8', '--verbose', 'file.dat']
change = {'--number': '-n', '--verbose': '-v'}
  .each {|k, v| ARGS.map! {|p| (p==k.to_s) ? v : p} }
p ARGS