Tuesday, January 29, 2013
Birthday Spanking and Loop lesson learned
So I learned a little date math today. I was able to figure out when I will be one billion seconds old, then forgot. Sometime in 2018, I'll run the code again before then ;-). This date math involved asking what year you are born and then gets.chomp for the answer on the command line with an until loop to round it out by putsing the number of spanks for how many years old you are.
The obvious fact that I have been meaning to write down that I forget often (like left and right hand when you first learned) is this:
puts INSIDE loop vs. puts OUTSIDE loop. You puts value INSIDE loop when you want to see exactly what is happening and the values being produced by the loop. If you would rather just see the answer at the end, THAT is when you put the value outside of the loop.
For example: I would puts inside a loop if I wanted to watch the fibonnaci sequence calculate from one to a billion. I would puts OUTSIDE a loop if I wanted to see the last fibonnaci number under one billion and wanted a single value.
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment