Skip to content

Instantly share code, notes, and snippets.

View bheeshmar's full-sized avatar

Bheeshmar Redheendran bheeshmar

View GitHub Profile
@bheeshmar
bheeshmar / grep_bench.rb
Created April 9, 2011 22:10
Benchmarking various ways to grep recursively
require 'benchmark'
Benchmark.bm(15) do |test|
test.report('grep-recursive') do
system('grep -r --include="*.h" -l include /usr/src/linux-headers-2.6.32-30/ > /dev/null')
end
test.report('grep-find') do
system('grep -l include $(find /usr/src/linux-headers-2.6.32-30/ -name "*.h" -print) > /dev/null')
end
@bheeshmar
bheeshmar / conversions.rb
Created April 11, 2011 22:53
Behavior of different conversion operators provided by Kernel
values = [nil, '', [], {}, true, false, 0, 3.51, '0', '42', '0xb', '1.2', '1e2',
'nil', 'true', 'false' ,'foo', [3,4], {5=>6}]
converters = %w(String to_s Integer to_i Float to_f Array to_a)
table_format = "%9s" * converters.size + "\n"
printf( "%9s ", '')
legend = converters
printf(table_format, *legend)
values.each do |v|
@bheeshmar
bheeshmar / textaid.rb
Created February 7, 2012 22:28
Webserver for Textaid Chrome plugin
require 'webrick'
require 'tempfile'
# Start gvim in foreground mode
$EDITOR = "gvim -f "
s = WEBrick::HTTPServer.new(:Port => 9292)
%w(INT TERM).each { |signal| trap(signal) { s.shutdown } }
s.mount_proc("/") do |req,res|
t = Tempfile.open('textaid') { |f| f.write(req.body); f }
@bheeshmar
bheeshmar / gnome-terminal-colors-for-putty.reg
Created February 27, 2012 18:43 — forked from tjensen/gist:1912325
I finally got fed up enough with PuTTY's default ANSI colors that I customized them to match the gnome-terminal settings on Ubuntu 10.04
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]
"Colour6"="0,0,0"
"Colour7"="85,87,83"
"Colour8"="204,0,0"
"Colour9"="239,41,41"
"Colour10"="78,154,6"
"Colour11"="138,226,52"
"Colour12"="196,160,0"
module Foo
def included(base)
base.extend ClassMethods
# We need to call these AR methods ON the class
base.class_eval do
after_commit :foo
end
end
module ClassMethods
def counts_of_files_by_extension(dir)
Dir["#{dir}/**/*"].reduce(Hash.new(0)) do |extension_counts, filepath|
extension_counts[File.extname(filepath)] += 1
extension_counts
end
end
directory = ARGV.shift || Dir.pwd
counts = counts_of_files_by_extension(directory)
@bheeshmar
bheeshmar / dirmd5.go
Last active August 29, 2015 14:19
Print filetree of current directory with md5sums of files
package main
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@bheeshmar
bheeshmar / fact.exs
Created May 3, 2015 02:46
Many faces of Factorial in Elixir
defmodule Foo do
# Use case matching
def fact1(n) do
case n do
1 -> 1
_ -> n * fact1(n-1)
end
end
### MY CUSTOMIZATIONS
# Set hostname, etc
export DESIREDHOSTNAME=caelian
sudo scutil --set HostName $DESIREDHOSTNAME
sudo scutil --set LocalHostName $DESIREDHOSTNAME
sudo scutil --set ComputerName $DESIREDHOSTNAME
# Use homebrew, click bottom right button in dialog when prompted
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install git
@bheeshmar
bheeshmar / splitwords.bash
Last active August 29, 2015 14:27
Word splitter solution in bash
#!/bin/bash
# call with ./splitwords.bash thisisawesome
find_substrings() {
local remainder=$1
matched=$2
# echo "in find_substrings '${remainder}' (${matched[@]})"
if [[ -z $remainder ]]; then
echo "Found: ${matched[@]}"
exit 0