Given my current state at my job, I am strangely driven to document the occasion every time I learn something momentous, especially when the learning of said momentous nugget of wisdom came at the expense of multiple hours of my frustrating work and self-loathing.
Today, with some gentle prodding from Jason, I was able to wrangle a previously insurmountable obstacle of organizing all amounts in a column of a table with a nested route recognized, sorted, counted, and summed, all down into a simple, one-line, 47-character piece of code.
Here today, for your viewing enjoyment, is the evolution of Chris’ ruby learning. Enjoy.
def additions
@additions = @register.additions
end
This was utter nonsense here, disregard
It then evolved to something that is at least appropriate:
def additions
self.additions.collect{ |addition| addition.amount }
end
Which was technically right, but I needed to not only list all the amounts of the additions in the table but add them together, so I extended:
def additions
self.additions.collect{ |addition| addition.amount }.inject( 0 ) { |sum,x| sum+x }
end
Which worked perfectly, passed the unit tests I had written for it to pass, and all was well in the world. And only then, after I nearly lost faith in myself as a human being, Jason step in to give me a hand-out. This could be much simpler!
def additions
additions.map(&:amount).inject( 0 ) { |sum,x| sum+x }
end
Now, you may be saying to yourself “Why should I care what this guy is learning, its just snippets of code from some program, to me,” and you’d be right. However I have a tendency to lose examples of my learning work in the multiple practice apps I have lounging about all over my computer, so for me, I can just post things I learn here so I can find them easier… that is of course unless the code bit I learned is sensitive in a work-sense, then I can’t post it and I guess I am just screwed, huh?
So come on, who’s proud of me?