Skip to content

Instantly share code, notes, and snippets.

@sebabelmar
Last active Mar 23, 2016
Embed
What would you like to do?
def do_twice
yield
yield
end
# The basics
do_twice {puts "HOLA"}
def do_something_to_an_array (array)
yield(array)
end
# Basics + Arguments
do_something_to_an_array([1,2,3,]) {|x| puts "Yeah... #{x}"}
def do_something_to_an_array_elements (arr)
i=0
while i < arr.length
# We are gonna yield code instead of using a pre-define method
this_is_the_yield(arr[i])
# You can comment line 23 and use line 25 and execute 36 instead of 35...
# yield(arr[i])
i+=1
end
end
def this_is_the_yield (some_sweet)
puts "im doing something with the element of the array #{some_sweet}"
end
# Yeah!! One way to use this...
do_something_to_an_array_elements([1,2,3,])
# do_something_to_an_array_elements([1,2,3,]) {|y| puts y**2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment