In a blog post on unconditional programming, Michael Perot shows how bounding operators ifcan be used as a tool to reduce code complexity.
He uses a concrete example to illustrate his point. Now I was thinking of other specific examples that could help me learn more about unconditional / ifless / forless programming.
For example, using OptionParser I made a cat that will set the stream if the switch is set --upcase:
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: cat [options] [file ...]"
opts.on("-u", "--upcase", "Upcase stream") do
options[:upcase] = true
end
end.parse!
if options[:upcase]
puts ARGF.read.upcase
else
puts ARGF.read
end
How can I handle this switch without a block if..else?
.