Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Created April 23, 2013 15:31
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 mjhea0/5444573 to your computer and use it in GitHub Desktop.
Save mjhea0/5444573 to your computer and use it in GitHub Desktop.
Find the sum of all the multiples of 5 or 7 below 100
# create a Proc to use with a Block
multiples = Proc.new do |n|
n % 5 == 0 || n % 7 == 0
end
# turn the Proc into a Block
array = (1..99).to_a.select(&multiples)
# sum the members of the array
array.inject{|sum,x| sum + x }
# reuse the Proc (remember Procs are Objects, Blocks are not, so they can be used over and over)
array2 = (1..999).to_a.select(&multiples)
array2.inject{|sum,x| sum + x }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment