Skip to content

Instantly share code, notes, and snippets.

@sebabelmar
Last active March 23, 2016 23:58
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 sebabelmar/2bca9bf53cc0296db546 to your computer and use it in GitHub Desktop.
Save sebabelmar/2bca9bf53cc0296db546 to your computer and use it in GitHub Desktop.
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