-
-
Save dgholz/8313960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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