Skip to content

Instantly share code, notes, and snippets.

View whitehat101's full-sized avatar

Jeremy whitehat101

View GitHub Profile
@whitehat101
whitehat101 / carsar.rb
Created August 22, 2016 22:07
Print all 26 iterations of Carsar's Chiper for a string
# some credit: https://gist.github.com/matugm/db363c7131e6af27716c
# Print all 26 iterations of Carsar's Chiper for a string
def caesar_cipher(string, shift = 1)
alphabet = Array('a'..'z')
non_caps = Hash[alphabet.zip(alphabet.rotate(-shift))]
alphabet = Array('A'..'Z')
caps = Hash[alphabet.zip(alphabet.rotate(-shift))]
@whitehat101
whitehat101 / factorial.rb
Created June 2, 2016 00:54
Adds :fact to Fixnum
module WhiteHat101
module Factorial
# returns 1 for input < 1
def fact
(1..self).inject 1, :*
end
alias_method :!, :fact
end
end
def time_to_angle h=0, m=0
puts "hours: #{h*30+m/2}"
puts "mins: #{m*6}"
end
@whitehat101
whitehat101 / wordlist.rb
Created May 31, 2016 00:59
Ubuntu Word list Iteration
# Wordlist on most Ubuntu Installs
open '/usr/share/dict/american-english' do |list|
until list.eof?
word = list.gets.chomp
next unless word.match /^[A-Za-z]+$/
value = word.upcase.split(//).inject(1) do |sum,v|
sum *= v.ord - 'A'.ord + 1
end
# puts "#{word} = #{value}"
@whitehat101
whitehat101 / ws_channels.rb
Created May 20, 2016 23:30
Simple WebSocket Channel Managment
require 'set'
class SingleChannel
def initialize id
@id = id
@sockets = Set.new
end
def push ws
@sockets << ws
end
include Process
# puts " ROOT: #{pid} of #{ppid}"
# at_exit { puts " EXIT: #{pid} of #{ppid}" }
$running = true
$workers = []
def start_worker
$workers << fork do
Signal.trap "INT", "DEFAULT"
RealStdOut = $stdout
# Replace the main thread's $stdout and $stderr with IO.pipes
# Create a thread to watch for data on the pipes
# call given block with IO name, timestamp, and data read from IO
def redirect_stdout_and_stderr_to &callback
rout, $stdout = IO.pipe
rerr, $stderr = IO.pipe
map = { rout => 'stdout', rerr => 'stderr' }
@whitehat101
whitehat101 / vector_2d.rb
Last active April 13, 2016 00:08
Vector2D
class Vector2D
include Math
attr_accessor :x, :y
def self.magnitude_angle magnitude, angle
x = magnitude * cos(angle*PI/180)
y = magnitude * sin(angle*PI/180)
new x, y
end
def initialize x, y
@x = x; @y = y
@whitehat101
whitehat101 / circle.rb
Created March 12, 2016 00:43
A Ruby Circle Class
module WhiteHat101
class Circle
attr_accessor :origin, :radius
def initialize origin = [0,0], radius = 0
@origin = origin
@radius = radius
end
def self.origin_point origin, point
radius = Math.sqrt origin.zip(point)
.map {|a,b| (a - b)**2 }
@whitehat101
whitehat101 / mean.rb
Last active May 2, 2016 18:31
Add :mean to Arrays
module WhiteHat101
module Mean
def mean
inject(0.0, :+) / length
end
alias avg mean
alias average mean
def mean_absolute_deviation
mean = self.mean