Skip to content

Instantly share code, notes, and snippets.

@timrandg
Created October 25, 2013 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timrandg/7157520 to your computer and use it in GitHub Desktop.
Save timrandg/7157520 to your computer and use it in GitHub Desktop.
sending to block
public
def average_by_every(arr, q)
arr.each_slice(q).collect{|q_set| q_set.inject{|s,n| s+n}/q }
end
p average_by_every([1,2,3,4,5,6,7,8,9], 2)
p average_by_every([1,2,3,4,5,6,7,8,9], 3)
@timrandg
Copy link
Author

returns:
[1, 3, 5, 7, 4]
[2, 5, 8]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment