Skip to content

Instantly share code, notes, and snippets.

@granolocks
granolocks / proc_tracker.rb
Last active December 14, 2015 22:28
proc_tracker.rb
require 'json'
class Tracker
def procs
[
Proc.new { |i| i ** -3 },
Proc.new { |i| i ** -2 },
Proc.new { |i| i ** -1 },
Proc.new { |i| i ** 0 },
Proc.new { |i| i ** 1 },
Proc.new { |i| i ** 2 },
@granolocks
granolocks / interpolation.rb
Last active December 14, 2015 23:18
ruby variable interpolation stufffssss
1.9.3p385 :001 > $a = @a = A = a = "ah"
=> "ah"
1.9.3p385 :002 > "#$a #{$a} $a"
=> "ah ah $a"
1.9.3p385 :003 > "#@a #{@a} @a"
=> "ah ah @a"
1.9.3p385 :004 > "#A #{A} A"
=> "#A ah A"
1.9.3p385 :005 > "#a #{a} a"
=> "#a ah a"
@granolocks
granolocks / timer.rb
Last active December 15, 2015 05:59
motivational timer
#!/usr/bin/env ruby
# I use this on Crunchbang and xubuntu systems.
# May need to be modified depending on availability of notify-send as well as icons
def time
sleep 300
end
def notify(urgency, icon, title, message)
`notify-send -u #{urgency} -i #{icon} '#{title}' '#{message}'`
@granolocks
granolocks / thingy.rb
Created April 17, 2013 01:08
pseudo code
class AppConfig < ActiveRecord::Base # Or whatever, its a model
attr_accessible :homepage_template, :connection_password, :i_dunno_whatever
# bear with me, its all pseudo code
validates :homepage_template, :presence => true, :inclusion => { :in => MagicalTemplateHelper.homepage_templates }
validates_presence_of :connection_password
# settings for arbitrary shit we need to set
# these are way hard to validate
# and require a lot of conditional b/s
@granolocks
granolocks / recursive_functions.erl
Last active December 18, 2015 03:19
Most of the functions in this module are inspired by the guides in this site: http://learnyousomeerlang.com/
-module(recursive_functions).
-export([
factorial/1,
tail_fac/1,
tail_fac/2,
recursive_length/1,
tail_len/1,
tail_len/2,
duplicate/2,
tail_duplicate/2,
@granolocks
granolocks / json_diff_and_patch.rb
Last active December 23, 2015 19:49
JSON Patching and Diffing..
require 'jsondiff' # https://github.com/francois2metz/jsondiff
require 'json/patch' # https://github.com/guillec/json-patch
def test_1
hash_1 = {a: {b:"sexy key"}, c: [1,2,3], x: "gonna remove this one"}
json_1 = JSON.generate(hash_1)
hash_2 = {a: {b:"new key", b2: "sexier key"}, c: [2,3,4], d: "a new key entirely"}
json_2 = JSON.generate(hash_2)
@granolocks
granolocks / klassmethods.rb
Created September 24, 2013 21:01
class methods for kevin
class MySexyClass
def self.this_is_a_class_method
"class method called"
end
def this_is_an_instance_method
"instance method called"
end
end
@granolocks
granolocks / gist:7885555
Last active December 30, 2015 21:09
benchmark unix versus bash vol1
require 'benchmark'
puts <<desc
Read the String
Ruby: File.read
Unix: cat
desc
Benchmark.bm do |bmark|
bmark.report(:ruby) do
@granolocks
granolocks / bash__DATA__.sh
Created January 16, 2014 22:10
read DATA from end of bash script... :)
f_data(){
tail -n $(expr $(wc -l $BASH_SOURCE | cut -d " " -f 1) - $(grep -ne '#\sDATA' $BASH_SOURCE|cut -d ":" -f 1)) $BASH_SOURCE
}
f_data
# DATA
# test data 1
# test data 2
# test data 3
@granolocks
granolocks / verify.sh
Last active January 3, 2016 12:49
Verify Y / N
f_verify(){
echo "1. Yes"
echo "2. No"
read -p "Are you sure?: " input
matches=$(echo $input | grep -ie "^[Y|1]")
if [ $? -ne 0 -a -n $matches ]; then
echo "You must enter 'yes' to continue."
exit