Created
March 19, 2013 16:10
-
-
Save maker-leo/5197437 to your computer and use it in GitHub Desktop.
Some examples of blocks, including our own implementation of select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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