Skip to content

Instantly share code, notes, and snippets.

@granolocks
granolocks / interesting.rb
Created April 3, 2012 16:00
Transpose + Zip == ??
# Note, this is getting run from a Rails console.
# The results are a bit surprising.
t = User.last.attributes.to_a.transpose
# This returns an Array with 2 sub-arrays containing all attribute keys and values respectively
# => [["work_phone", ... ], ["12345678910", ...]]
Hash[t[0].zip(t[1])] == User.last.attributes
# => true
@granolocks
granolocks / ubuntu_install.sh
Created April 4, 2012 15:24
Ubuntu 11.10 install notes
# Ubuntu 11.10 Setup Notes:
# Note, this isn't really a script, i just called it .sh to get syntax highlighting
# prep
sudo apt-get update
# setup git and ssh
# Add key to github.
ssh-keygen
@granolocks
granolocks / ifconfig.rb
Created April 23, 2012 14:26
Simple Ifconfig Parser in Ruby
class IfconfigParser
MAC_REGEX = /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/
# NOTE: this has some slightly weird results for wlan interfaces. Really meant to be used for eth0
def self.ifconfig(iface)
output = Hash.new
ifconfig_string = `ifconfig #{ iface }`
ifconfig_string.gsub!("RX ", "RX_")
ifconfig_string.gsub!("TX ", "TX_")
@granolocks
granolocks / regex.rb
Created May 3, 2012 01:30
Useful Regex
VALID_IP_ADDRESS = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
VALID_HOSTNAME = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/
@granolocks
granolocks / multi_value_hash.rb
Created May 6, 2012 20:14
Multi-value hashes in Ruby
# Multi-value hashes in Ruby
# Posted on March 24, 2011
# http://matthew.mceachen.us/blog/multi-value-hashes-in-ruby-1114.html
# Any non-trivial project quickly finds itself needing data structures that are more exotic than simple arrays or maps. In my quest for multimap
# nirvana, I first found references where every value-put call would need to be changed to look like this:
h = {}
(h[:key] ||= []) << "value 1"
(h[:key] ||= []) << "value 2"
puts h
@granolocks
granolocks / header markup.html
Created August 17, 2012 18:05
header markup.html
<!DOCTYPE html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8" />
#!/bin/sh
# This checks that the specified file is less than 4 days old
# deletes files that are too old
for f in *.jpg
do
FILE=$f
MAXAGE=$(bc <<< '4*24*60*60') # seconds in a week
#MAXAGE=$(bc <<< '60') # seconds in a minute, for testing
@granolocks
granolocks / app.rb
Created September 20, 2012 15:13
app.rb
class Application < Sinatra:: Base
def initialize
@xinstance = 'first'
super
end
get '/1' do
@xinstance = "hit 1"
# returns "hit 1"
@granolocks
granolocks / params.rb
Created September 27, 2012 16:17
shell words escape
require 'rubygems'
require 'sinatra'
require 'pry'
# run with:
# ruby params.rb
# test route example: http://localhost:4567/blah/blee!!;;;
get '/:test/:test2' do
sanitize_params
require 'rubygems'
require 'net/ssh'
# Run this on the machine (node) which needs to tunnel out to forward the UI to the remote system (console)
Net::SSH.start("remote_host", "remote_user") do |ssh|
# since we are running sinatra locally we will forward 43210 on the remote_host to our localhost 4567
# This is effectively the same as:
# ssh -R 4567:localhost:43210 remote_user@remote_host
ssh.forward.remote(4567, "localhost", 43210)
ssh.loop { true }