Skip to content

Instantly share code, notes, and snippets.

View tlehman's full-sized avatar

Tobi Lehman tlehman

View GitHub Profile
@tlehman
tlehman / lan_ip.rb
Created August 27, 2015 18:03
LAN IP
puts UDPSocket.open { |s| s.connect('www.example.com', 1); s.addr.last }
@tlehman
tlehman / jth_bit.c
Created February 9, 2014 18:18
jth_bit in C
#include <stdio.h>
#define TWO_TO_THE(POWER) (1 << POWER)
#define JTH_BIT(BITS, J) ((TWO_TO_THE((J-1)) & BITS) != 0)
int main(int argc, char *argv[]) {
printf("JTH_BIT(8, 1) = %d\n", JTH_BIT(8, 1));
printf("JTH_BIT(8, 2) = %d\n", JTH_BIT(8, 2));
printf("JTH_BIT(8, 3) = %d\n", JTH_BIT(8, 3));
printf("JTH_BIT(8, 4) = %d\n", JTH_BIT(8, 4));
@tlehman
tlehman / just_ip_addrs.sh
Last active August 29, 2015 14:04
Just IP addresses
#!/bin/bash
# extract IP addresses from ifconfig command using Perl
if [ "$1" = "-v6" ]; then
ifconfig | perl -ne '/inet6 addr: (([0-9a-f]*:?)*\/\d+)/ && print "$1\n"'
else
ifconfig | perl -ne '/addr:((\d{1,3}\.?){4})/ && print "$1\n"'
fi
Verifying myself: My Bitcoin username is +tlehman. https://onename.io/tlehman
@tlehman
tlehman / gpa.rb
Created October 21, 2014 19:25
GPA calculation
def gpa(grades)
points = {:A => 4.0, :B => 3.0, :C => 2.0, :D => 1.0, :F => 0}
grade_values = grades.merge(points) { |grade,value,number| (number.to_f)*value }
numer = grades.values.inject(:+).to_f
denom = grade_values.values.inject(:+).to_f
((numer/denom)*10).round(3)
end
# ==> nil
@tlehman
tlehman / proc_lambda_block.rb
Created October 22, 2014 22:07
procs vs lambdas (making ruby blocks into objects using proc or lambda)
a = proc { return 1 }
# ==> #<Proc:0x007f96d280a118@(irb):1>
b = lambda { return 1 }
# ==> #<Proc:0x007f96d3002438@(irb):3 (lambda)>
a.call
# =LocalJumpError: unexpected return
# = from (irb):1:in `block in irb_binding'
# = from (irb):5:in `call'
@tlehman
tlehman / multco_election_2014.sh
Last active August 29, 2015 14:08
Multnomah County Election Results Data
#!/bin/sh
curl -s https://multco.us/elections/november-2014-general-election-results | ruby -e 'STDIN.readlines.each {|line| puts line if /<pre>/ =~ line .. /<\/pre>/ =~ line }'
@tlehman
tlehman / superclasses.rb
Created November 14, 2014 01:27
Superclasses and classes
# Starting with a simple String object:
hello = "Hello, World"
def output_class_hierarchy(object)
o = object
while o.class!=Class || (o != nil && o != NilClass)
if o.is_a? Class
puts " #{o} -> #{o.superclass.inspect} [label=superclass,color=red];"
o = o.superclass
else
@tlehman
tlehman / pad_filenames_zeros
Created November 26, 2014 20:10
pad filenames left with zeros
→ ls
file1.txt file14.txt file19.txt file23.txt file28.txt file32.txt file37.txt file41.txt file46.txt file50.txt file55.txt file6.txt
file10.txt file15.txt file2.txt file24.txt file29.txt file33.txt file38.txt file42.txt file47.txt file51.txt file56.txt file7.txt
file11.txt file16.txt file20.txt file25.txt file3.txt file34.txt file39.txt file43.txt file48.txt file52.txt file57.txt file8.txt
file12.txt file17.txt file21.txt file26.txt file30.txt file35.txt file4.txt file44.txt file49.txt file53.txt file58.txt file9.txt
file13.txt file18.txt file22.txt file27.txt file31.txt file36.txt file40.txt file45.txt file5.txt file54.txt file59.txt
~/tmp/files 2014-11-26 12:09:55
→ for f in $(ls); do newnum=$(ruby -e "/file(?<num>\d+).txt/ =~ '$f'; puts num.rjust(2,'0')"); mv $f file$newnum.txt; done
mv: ‘file10.txt’ and ‘file10.txt’ are the same file
mv: ‘file11.txt’ and ‘file11.txt’ are the same file
@tlehman
tlehman / git-step-through-commits-follow.sh
Created November 26, 2014 21:30
step through git commits on a file:
git log --pretty="%H" --follow main.py | for commit in $(cat -); do git show $commit; read; done