Last active
August 29, 2015 14:23
-
-
Save acmei/bdc0b098117cd72a1b1a to your computer and use it in GitHub Desktop.
Learn to Program Ch 14 - Blocks and Procs
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
# Extracting multiples of 3 | |
multiples_of_3 = Proc.new do |n| | |
n % 3 == 0 | |
end | |
(1..100).to_a.select(&multiples_of_3) | |
# Round down method | |
floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911] | |
round_down = Proc.new { |n| n.floor } | |
ints = floats.collect(&round_down) | |
# EXAMPLE 1 ------------------------------------ | |
# toast = Proc.new do | |
# puts "Cheers!" | |
# end | |
# toast.call | |
# toast.call | |
# toast.call | |
# EXAMPLE 2 ------------------------------------ | |
# do_you_like = Proc.new do |good_stuff| | |
# puts "I *really* like #{good_stuff}!" | |
# end | |
# do_you_like.call 'chocolate' | |
# do_you_like.call 'figs' | |
# EXAMPLE 3 ------------------------------------ | |
# def do_self_importantly some_proc | |
# puts "Everybody just HOLD ON! I'm doing something..." | |
# some_proc.call | |
# puts "OK everyone, I'm done. As you were." | |
# end | |
# say_hello = Proc.new do | |
# puts "hello" | |
# end | |
# say_goodbye = Proc.new do | |
# puts "goodbye" | |
# end | |
# do_self_importantly say_hello | |
# do_self_importantly say_goodbye | |
# EXAMPLE 4 ------------------------------------ | |
# def maybe_do some_proc | |
# if rand(2) == 0 | |
# some_proc.call | |
# end | |
# end | |
# def twice_do some_proc | |
# some_proc.call | |
# some_proc.call | |
# end | |
# wink = Proc.new do | |
# puts "<wink>" | |
# end | |
# glance = Proc.new do | |
# puts "<glance>" | |
# end | |
# maybe_do wink | |
# maybe_do wink | |
# maybe_do glance | |
# maybe_do glance | |
# twice_do wink | |
# twice_do glance | |
# EXAMPLE 5 ------------------------------------ | |
# def do_until_false first_input, some_proc | |
# input = first_input | |
# output = first_input | |
# while output | |
# input = output | |
# output = some_proc.call input | |
# end | |
# input | |
# end | |
# build_array_of_squares = Proc.new do |array| | |
# last_number = array.last | |
# if last_number <= 0 | |
# false | |
# else | |
# # Take off the last number... | |
# array.pop | |
# # ...and replace it with its square... | |
# array.push last_number*last_number | |
# # ...followed by the next smaller number. | |
# array.push last_number-1 | |
# end | |
# end | |
# always_false = Proc.new do |just_ignore_me| | |
# false | |
# end | |
# puts do_until_false([5], build_array_of_squares).inspect | |
# yum = 'lemonade with a hint of orange blossom water' | |
# puts do_until_false(yum, always_false) | |
# EXAMPLE 6 ------------------------------------ | |
# def compose proc1, proc2 | |
# Proc.new do |x| | |
# proc2.call(proc1.call(x)) | |
# end | |
# end | |
# square_it = Proc.new do |x| | |
# x * x | |
# end | |
# double_it = Proc.new do |x| | |
# x + x | |
# end | |
# double_then_square = compose double_it, square_it | |
# square_then_double = compose square_it, double_it | |
# puts double_then_square.call(5) | |
# puts square_then_double.call(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment