Created
April 10, 2014 20:33
-
-
Save catm705/10420518 to your computer and use it in GitHub Desktop.
Procs, Methods, Blocks, Functions
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 log_before_doing() | |
# puts "About to do stuff in a block." | |
# yield | |
# yield | |
# end | |
# log_before_doing() { puts "Doin' stuff in a block."} | |
# def do_ten_times() | |
# 10.times { yield } | |
# end | |
# do_ten_times() { puts "YEAH!" } | |
# def do_ten_times_explicit(&block) | |
# 10.times { block.call() } | |
# end | |
# do_ten_times_explicit() { puts "This is a block." } | |
# puts | |
# def tell_me_about_the_block(&some_block) | |
# puts some_block.inspect | |
# puts some_block.class | |
# if some_block | |
# some_block.call() | |
# end | |
# return 1 | |
# end | |
# puts tell_me_about_the_block(){ puts "YO!" }.class | |
# def call_block_if_given | |
# if block_given? | |
# yield | |
# end | |
# end | |
# call_block_if_given() | |
# tell_me_about_the_block() { puts "This is also a block." } | |
# Can't do this: | |
#some_block = { puts "I'm in a block!" } | |
# CAN do this: | |
# some_block = Proc.new { puts "I'm in a block!" } | |
# some_block.call() | |
# tell_me_about_the_block(some_block) | |
# [1, 2, 3].each do |item| | |
# puts("Iterating over #{item}...") | |
# end | |
# [1, 2, 3].each(&Proc.new { |item| puts("Iterating over #{item}...") }) | |
# def my_each(items, &proc) | |
# amt_items = items.size() | |
# counter = 0 | |
# while counter < amt_items | |
# current_item = items[counter] | |
# proc.call(current_item) | |
# counter = counter + 1 | |
# end | |
# end | |
# def my_each(items) | |
# amt_items = items.size() | |
# counter = 0 | |
# while counter < amt_items | |
# current_item = items[counter] | |
# yield(current_item) | |
# counter = counter + 1 | |
# end | |
# items | |
# end | |
# my_each([1, 2, 3]) do |item| | |
# puts("Iterating over #{item}...") | |
# end | |
# def do_for_all_instructors | |
# yield("Phil") | |
# yield("Max") | |
# yield("David") | |
# end | |
# def do_for_all_instructors(&block) | |
# block.call("Phil", "GA lobby") | |
# block.call("Max", "Mars") | |
# block.call("David", "Union Square") | |
# end | |
# do_for_all_instructors do |instructor, location| | |
# puts "Hang out with #{instructor} (after the course) at #{location}." | |
# end | |
def get_maxs_age | |
log_file = "age_query.log" | |
log = File.open(log_file, 'a+') | |
log.puts("#{Time.now}: Someone asked for Max's age.") | |
return 500 | |
end | |
# Input: FixNum | |
# Side Effect: | |
# - Puts 5 lines | |
# def ask_about_maxs_age_without_call(age) | |
# puts age.class | |
# puts age.inspect | |
# puts "Bill asks; it's #{age}." | |
# puts "John asks; it's #{age}." | |
# puts "Rick asks; it's #{age}." | |
# end | |
#ask_about_maxs_age_without_call(get_maxs_age()) | |
# Input: Proc | |
# Side Effect: | |
# - Puts 5 lines | |
# - Calls the "get_maxs_age" method Three times | |
def ask_about_maxs_age_with_call(get_age) | |
puts get_age.class | |
puts get_age.inspect | |
puts "Bill asks; it's #{get_age.call}." | |
puts "John asks; it's #{get_age.call}." | |
puts "Rick asks; it's #{get_age.call}." | |
end | |
ask_about_maxs_age_with_call(method(:get_maxs_age)) | |
------------------------------------------------------------------------------------------ | |
def my_map(items) | |
amt_items = items.size() | |
counter = 0 | |
new_arr = [] | |
while counter < amt_items | |
current_item = items[counter] | |
new_arr << yield(current_item) | |
counter += 1 | |
end | |
return new_arr | |
end | |
# TESTING IN PRY WITH CODE BELOW: | |
# new_array = my_map([items]) { items * 2 } | |
# OTHER WAY | |
def my_map(items) | |
amt_items = items.size() | |
counter = 0 | |
new_arr = [] | |
while counter < amt_items | |
current_item = items[counter] | |
new_arr << yield(current_item) | |
counter += 1 | |
end | |
return new_arr | |
end | |
------------------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment