Skip to content

Instantly share code, notes, and snippets.

@maker-leo
Created March 19, 2013 16:10
Show Gist options
  • Save maker-leo/5197437 to your computer and use it in GitHub Desktop.
Save maker-leo/5197437 to your computer and use it in GitHub Desktop.
Some examples of blocks, including our own implementation of select
def do_some_maths
puts yield(10)
puts yield(20)
end
do_some_maths do |num|
num * 20
end
do_some_maths do |number|
number - 5 * 35
end
def select(arr)
selected_values = []
arr.each do |value|
if yield(value)
selected_values << value
end
end
selected_values
end
arr = [1,2,3,4]
select(arr) { |array_value| array_value.odd? }
select(arr) { |array_value| array_value.even? }
puts select(arr) { |array_value| array_value > 2 }.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment