I hereby claim:
- I am redfred7 on github.
- I am fredh (https://keybase.io/fredh) on keybase.
- I have a public key ASDPgR7rqQ2qe6LrGMOjNYA88-WlHoSXqUA6TFrQ8dop8wo
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
| require 'benchmark' | |
| @arr = (1..100000).to_a | |
| def rookie_way | |
| new_arr = [] | |
| @arr.each do |x| | |
| if x % 2 == 0 | |
| new_arr << x * 3 if x * 3 < 20 |
| require 'benchmark' | |
| def factorial(x) | |
| x == 1 ? 1 : x * factorial(x-1) | |
| end | |
| factorial_hash ||= Hash.new do |hash,key| | |
| key == 1 ? (key = 1) : (hash[key] = key * hash[key-1]) | |
| end |
| require 'benchmark' | |
| # using simple conditional statements | |
| def conditional(n) | |
| if (n % 3 == 0) && (n % 5 != 0) | |
| 'Fizz' | |
| elsif (n % 3 != 0) && (n % 5 == 0) | |
| 'Buzz' | |
| elsif (n % 3 == 0) && (n % 5 == 0) | |
| 'FizzBuzz' |
| ####### PROXY PATTERN ############# | |
| ### way #1 | |
| ## The following code implements the proxy pattern in the traditional | |
| ## manner, i.e. the proxy mirrors the methods of the real object (e.g | |
| ## drive) and delegates accordingly | |
| class RealCar | |
| def drive | |
| puts "vroom,vroom..." |
| ####### COMMAND PATTERN ############# | |
| ### 1st attempt | |
| ## The following code implements the command pattern in the traditional | |
| ## manner, i.e. with a separate class for each command. We aim to | |
| ## implement a do / undo application that increments a global aggregate | |
| ## (or decrements it for the Undo) | |
| class Command | |
| def do_command |
| ####### STRATEGY PATTERN ############# | |
| ### way #1 | |
| ## The following code implements the strategy pattern in the traditional | |
| ## manner, i.e. with a separate class for each strategy (xxxPayment), | |
| ## which is then passed in the constructor of the Context class (in | |
| ## this case the Purchase class). The Context then selects a strategy | |
| ## at construction time and delegates its stragegy method (i.e. pay) to | |
| ## the strategy object |
| require 'dover_to_calais' | |
| EM.run do | |
| # use Control + C to stop the EM | |
| Signal.trap('INT') { EventMachine.stop } | |
| Signal.trap('TERM') { EventMachine.stop } | |
| DoverToCalais::API_KEY = 'my_opencalais_key' |
| require 'dover_to_calais' | |
| EM.run do | |
| # use Control + C to stop the EM | |
| Signal.trap('INT') { EventMachine.stop } | |
| Signal.trap('TERM') { EventMachine.stop } | |
| DoverToCalais::API_KEY = 'my_opencalais_key' |