Skip to content

Instantly share code, notes, and snippets.

View sstelfox's full-sized avatar

Sam Stelfox sstelfox

View GitHub Profile
@sstelfox
sstelfox / running_avg_test.rb
Created December 29, 2014 20:22
A quick test comparing real average versus a running average
count = 0
running_avg = nil
real_avg = nil
values = []
50000.times do |n|
count += 1
new_value = rand(1000.to_f)
values.push(new_value)
@sstelfox
sstelfox / longest_line.rb
Created January 9, 2015 17:33
When passing in a list of file names this will return the file, location and length of the longest line. Useful in identifying the longest line of code in a repo using something like: `find . -name '*.rb' -print | ruby longest_line.rb`
#!/usr/bin/env ruby
file_line_length = {}
STDIN.read.split("\n").each do |f|
file_line_length[f] = {length: -1, line_num: nil}
fh = File.open(f)
fh.each_line do |l|
if file_line_length[f][:length] < l.length
@sstelfox
sstelfox / arp_inspector.rb
Last active August 29, 2015 14:13
Playing around with manual packet inspection using ruby and pcaprub
#!/usr/bin/env ruby
require 'ipaddr'
require 'pcaprub'
require 'strscan'
# https://en.wikipedia.org/wiki/EtherType
ETHER_TYPE = {
0x0800 => :ipv4,
0x0806 => :arp,
@sstelfox
sstelfox / test.rb
Created January 20, 2015 14:46
Alternate instantiation
class Foo
def initialize(*args)
puts args.inspect
end
end
def Foo(*args)
Foo.new(*args)
end
@sstelfox
sstelfox / config.txt
Last active August 29, 2015 14:14
A safe bash config file loader using that prevents external commands from being run. This expects both files to be in the same directory.
# A good key that we want
VALID_CONFIG_KEY=testing
# We don't want external commands to be able to run
DANGEROUS=$(date)
# Or a weird syntax to trigger commands
BAD=$(echo 'test')=value
# And we should receive a warning about lines that don't match the key value
@sstelfox
sstelfox / serial_playground.rb
Created February 24, 2015 19:56
Non-blocking safe interaction with serial port.
require 'rubygems'
require 'serialport'
require 'thread'
serial_port = SerialPort.new('/dev/ttyUSB0', 9600, 8, 1, SerialPort::NONE)
read_queue = Queue.new
write_queue = Queue.new
read_thread = Thread.new do
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
@sstelfox
sstelfox / index_locator.rb
Created March 11, 2015 19:00
I needed to uniquely identify a position in a three dimensional array of unique strings in both directions.
#!/usr/bin/env ruby
def x_values
%w(a b c d)
end
def y_values
%w(m n o p)
end
ExTest = Class.new(StandardError)
begin
raise ExTest, 'wee'
rescue ExTest => e
puts 'Caught 1'
raise e
rescue => e
puts 'Caught 2'
end
@sstelfox
sstelfox / forward_bench.rb
Created April 21, 2015 03:21
A quick test to see the performance impact a delegated method has compared to a direct instantiation.
#!/usr/bin/env ruby
require 'benchmark/ips'
require 'forwardable'
class Target
def goal(msg)
msg.reverse
end
end