Thursday, December 24, 2009

yield in Ruby took a while to sink in - Today, I used it for an implementation and thats when I understood it well - For the new year greeting program - to send automatic customized emails.

I had to send customized inline images in the email - for that I had to write an Image manipulation program. I used Ruby for that (Only because I am not comfortable with Haskell yet!) git://

So the Image class contains [] operators etc but for any function to be applied on the image, you typically need to go over each pixel and change it in some manner. Also, if the color value went out of the range (less than 0 or greater than 255), it had to be brought back into the range. I found myself uninterestingly repeating the act of traversing over the pixels and ensuring that the resulting value stayed in the range. That's when it occurred to me that I could abstract it out into the image class into a function called - apply function! - it looks like this

def applyFunction
@width.times do |x|
@height.times do |y|
(r,g,b)=(yield x,y,r,g,b).map! {|c| Image.normalizeColor c}