Skip to content

Instantly share code, notes, and snippets.

View RedFred7's full-sized avatar

Fred Heath RedFred7

View GitHub Profile
@RedFred7
RedFred7 / keybase.md
Last active January 13, 2021 22:27
Keybase proof

Keybase proof

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:

@RedFred7
RedFred7 / list_comprehensions.rb
Last active October 3, 2015 19:30
three different ways to create a list from another list
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
@RedFred7
RedFred7 / Memoization.rb
Created May 9, 2015 10:53
recursive memoization with hashes
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
@RedFred7
RedFred7 / FizzBuzz.rb
Created May 7, 2015 21:58
3 approaches to solving FizzBuzz
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'
@RedFred7
RedFred7 / design_patterns_proxy.rb
Created February 11, 2015 20:43
Design Patterns the Ruby way: proxy
####### 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..."
@RedFred7
RedFred7 / design_patterns_command.rb
Created February 11, 2015 20:42
Design Patterns the Ruby way: command
####### 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
@RedFred7
RedFred7 / design_patterns_strategy.rb
Created February 11, 2015 20:39
Design Patterns the Ruby way: strategy
####### 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
@RedFred7
RedFred7 / gist:6961853
Last active December 25, 2015 10:29
Use DoverToCalais to find all Persons or Organizations with a relevance score greater than 0.1, if the data source contains an environmental event.
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'
@RedFred7
RedFred7 / gist:6961349
Last active December 25, 2015 10:28
Using DoverToCalais to semantically tag all files in a directory
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'