Skip to content

Instantly share code, notes, and snippets.

@aberant
Created December 31, 2009 19:54
Show Gist options
  • Save aberant/266880 to your computer and use it in GitHub Desktop.
Save aberant/266880 to your computer and use it in GitHub Desktop.
# getting more power out of Enumerable
# this is to convert an RGB data structure, into RGBA
require 'enumerator'
RGB_WIDTH = 3
raw_data = [1,1,1,2,2,2,3,3,3,4,4,4]
# returns an enumerable object with the raw data split up into chunks of 3
pixels = raw_data.enum_slice( RGB_WIDTH )
# give each pixel an extra alpha byte
rgba_result = pixels.inject([]){|result, pixel| result = result + pixel + [0] }
puts rgba_result.inspect
# [1,1,1,0,2,2,2,0,3,3,3,0,4,4,4,0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment