Created
June 20, 2016 17:09
-
-
Save idimitrov07/943015905904db271cbf83e5691b3860 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 5.times { puts "Hello!" } | |
| ### | |
| fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] | |
| doubled_fibs = fibs.collect { |el| el * 2 } | |
| #### | |
| def block_test | |
| puts "We're in the method!" | |
| puts "Yielding to the block..." | |
| yield | |
| puts "We're back in the method!" | |
| end | |
| block_test { puts ">>> We're in the block!" } | |
| ##### | |
| def yield_name(name) | |
| puts "In the method! Let's yield." | |
| yield("Kim") | |
| puts "In between the yields!" | |
| yield(name) | |
| puts "Block complete! Back in the method." | |
| end | |
| yield_name("Eric") { |n| puts "My name is #{n}." } | |
| yield_name("John") { |n| puts "My name is #{n}." } | |
| ################### | |
| def double(n) | |
| yield(n) | |
| end | |
| puts double(2) { |n| n * 2 } | |
| ########### | |
| multiples_of_3 = Proc.new do |n| | |
| n % 3 == 0 | |
| end | |
| (1..100).to_a.select(&multiples_of_3) | |
| ################# | |
| floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911] | |
| # Write your code below this line! | |
| round_down = Proc.new { |el| el.floor } | |
| # Write your code above this line! | |
| ####################### | |
| # Here at the amusement park, you have to be four feet tall | |
| # or taller to ride the roller coaster. Let's use .select on | |
| # each group to get only the ones four feet tall or taller. | |
| group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7] | |
| group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0] | |
| group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2] | |
| # Complete this as a new Proc | |
| over_4_feet = Proc.new { |height| height >= 4 } | |
| # Change these three so that they use your new over_4_feet Proc | |
| can_ride_1 = group_1.select(&over_4_feet) | |
| can_ride_2 = group_2.select(&over_4_feet) | |
| can_ride_3 = group_3.select(&over_4_feet) | |
| ###################### | |
| def greeter | |
| yield | |
| end | |
| phrase = Proc.new { puts "Hello there!" } | |
| greeter(&phrase) | |
| ####################### | |
| hi = Proc.new { puts "Hello!" } | |
| hi.call | |
| ################### | |
| numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| strings_array = numbers_array.map(&:to_s) | |
| ##################### | |
| def lambda_demo(a_lambda) | |
| puts "I'm the method!" | |
| a_lambda.call | |
| end | |
| lambda_demo(lambda { puts "I'm the lambda!" }) | |
| ############## | |
| strings = ["leonardo", "donatello", "raphael", "michaelangelo"] | |
| # Write your code below this line! | |
| symbolize = lambda { |param| param.to_sym } | |
| # Write your code above this line! | |
| symbols = strings.collect(&symbolize) | |
| ############### | |
| def batman_ironman_proc | |
| victor = Proc.new { return "Batman will win!" } | |
| victor.call | |
| "Iron Man will win!" | |
| end | |
| puts batman_ironman_proc | |
| def batman_ironman_lambda | |
| victor = lambda { return "Batman will win!" } | |
| victor.call | |
| "Iron Man will win!" | |
| end | |
| puts batman_ironman_lambda | |
| ##################### | |
| my_array = ["raindrops", :kettles, "whiskers", :mittens, :packages] | |
| # Add your code below! | |
| symbol_filter = lambda { |param| param.is_a? Symbol } | |
| symbols = my_array.select(&symbol_filter) | |
| ##################### | |
| odds_n_ends = [:weezard, 42, "Trady Blix", 3, true, 19, 12.345] | |
| ints = odds_n_ends.select { |el| el.is_a? Integer } | |
| ###################### | |
| ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44] | |
| # Add your code below! | |
| under_100 = Proc.new { |n| n < 100 } | |
| youngsters = ages.select(&under_100) | |
| #################### | |
| crew = { | |
| captain: "Picard", | |
| first_officer: "Riker", | |
| lt_cdr: "Data", | |
| lt: "Worf", | |
| ensign: "Ro", | |
| counselor: "Troi", | |
| chief_engineer: "LaForge", | |
| doctor: "Crusher" | |
| } | |
| # Add your code below! | |
| first_half = lambda { |key, value| value < "M" } | |
| a_to_m = crew.select(&first_half) | |
| ################### | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment