Skip to content

Instantly share code, notes, and snippets.

@dgholz
Forked from anonymous/gist:8308733
Last active January 2, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgholz/8313960 to your computer and use it in GitHub Desktop.
Save dgholz/8313960 to your computer and use it in GitHub Desktop.
def prompt(text=[])
text = [text].flatten
text.unshift('>').push(nil) # for trailing space
print text.join(' ')
return gets
end
def counter(maxcount)
numbers = []
for i in maxcount
puts "At the top of i is #{i}"
numbers.push(i)
i += 1
puts "Numbers now: #{numbers}"
puts "At the bottom of i is #{i}"
end
end
counter(0..prompt('how many?').to_i)
def counter2(maxcount)
numbers = []
# next line complicates unit testing a bunch ...
maxcount = 0..maxcount.to_i unless maxcount.respond_to? :each
for elem in maxcount
puts "At the top of maxcount is #{elem}"
numbers.push elem
puts "numbers now: #{numbers}"
end
end
counter2 prompt 'how many again? I forgot ...'
# more ruby
def prompt2( text=[], &block )
ret = prompt text
ret = block.call( ret ) if block_given?
return ret
end
prompt2 "once more, how many?" do |answer|
counter2 answer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment