What is a closure?
How do we implement/create/use a closure in Ruby?
What's the difference between a closure in JS vs Ruby?
Why do we use closures so much more in JS as opposed to Ruby?
What is the Ruby analog of an anonymous function in Javascript?
| // iOS Media Queries | |
| // Goal: capture styles for iPhone, iPhone 3G, iPhone 3GS, iPhone 4, iPhone 4S, iPad, and iPad 2 | |
| // | |
| // Author: Tony Schneider (@tonywok) | |
| // Please tell me where I fail. :) | |
| // iPhone v(4,4S) portrait | |
| // test: black text (overwritten by v* portrait) with blue background | |
| @media all and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) { | |
| a { |
What is a closure?
How do we implement/create/use a closure in Ruby?
What's the difference between a closure in JS vs Ruby?
Why do we use closures so much more in JS as opposed to Ruby?
What is the Ruby analog of an anonymous function in Javascript?
| require 'fis/test' | |
| class Phone | |
| def initialize(number) | |
| @number = number.gsub(/\D/, '') | |
| if @number.length == 11 && @number[0] == "1" | |
| @number = @number[1..10] | |
| end | |
| end |
| class MyApp | |
| def call(env) | |
| [200, {'Content-Type' => 'text/html'}, ['<a style="background-color:red;">URGENT MSG</a> from Tiny Tim: Hello']] | |
| end | |
| end |
| def check_prime(number) | |
| if number == 1 | |
| "You'd think you were prime, but you're not." | |
| elsif number == 2 | |
| "We call 2 the Littlest Prime" | |
| elsif number == 3 | |
| "Octavian Prime" | |
| elsif number == 4 | |
| "Ain't Prime" | |
| elsif number == 5 |
| code = "fx1=ZY&iX3=ZW&hm5=ZU&dg7=ZS&ei9=ZQ&ge11=ZO&tr13=ZM&hh15=ZK&uX17=ZI&si19=ZG&tX21=ZE&Xn23=ZC&hp25=ZA&pX27=YY&dl29=YW&wo31=YU&XX33=YS&XX35=YQ&re37=YO&et39=YM&ia41=YK&wn43=YI&uo45=YG&dX47=YE&so49=YC&ec51=YA&do53=XY&sX55=XW&xr57=XU&so59=XS&aX61=XQ&ph63=XO&ni65=XM&nX67=XK&Xa69=XI&yX71=XG&aX73=XE&ei75=XC&ie0=ZZ&Xa2=ZX&Xy4=ZV&aX6=ZT&Xn8=ZR&in10=ZP&hp12=ZN&Xa14=ZL&os16=ZJ&rx18=ZH&Xs20=ZF&od22=ZD&ce24=ZB&os26=YZ&Xd28=YX&ou30=YV&nw32=YT&ai34=YR&te36=YP&er38=YN&XX40=YL&XX42=YJ&ow44=YH&ld46=YF&Xp48=YD&ph50=YB&nX52=XZ&Xt54=XX&is56=XV&Xu58=XT&hh60=XR&rt62=XP&eg64=XN&ie66=XL&gd68=XJ&mh70=XH&Xi72=XF&xf74=XD" | |
| codev2 = code.split('&') | |
| codev3 = | |
| codev2.collect do |x| | |
| #split each string into two more strings | |
| x.split('=') | |
| end | |
| codev4 = codev3.flatten #turns array x arrays into a single array. |
| $basketball_game = | |
| { | |
| :team_1 => | |
| { | |
| :team_name => "Knicks", | |
| :colors => ["blue", "orange"], | |
| :player_1 => | |
| { | |
| :name => "Patrick Ewing", |
| library { | |
| :artist => { | |
| "The Magnetic Fields" => { | |
| "69 Love Songs" => ["Song 1", "Song 2"] | |
| ] | |
| "Get Lost" => ["Song 1", "Song 2"] | |
| } | |
| "Netural Milk Hotel" => { | |
| "In An Aeroplane Over the Sea" => ["Song 1", "Song 2"] | |
| } |
| # Temperature bot is comfortable when it's room temperature (18-21C). Help him out by completing the method below: | |
| def temperature_bot(temp) | |
| case temp | |
| when 18..21 | |
| "I like this temperature" | |
| else | |
| "This is uncomfortable for me" | |
| end | |
| end |
| # Chain at least 5 method calls to an object. Reduce this operation one chain at a time. | |
| puts "rotator dad rotator".reverse.slice(8,3).capitalize.slice(0,0).concat("Father's Day is this weekend.") | |
| # Don't just use the methods below, make up your own chain and try to see if you can do something fun with Ruby like in the example. | |
| puts "rotator dad rotator".reverse | |
| # #=> rotator dad rotator | |
| puts "rotator dad rotator".reverse.slice(8,3) |