Skip to content

Instantly share code, notes, and snippets.

1.9.3-p125 :036 > p = DotHash::Properties.new({})
=> #<DotHash::Properties:0x007f8cf408a308 @hash={}>
1.9.3-p125 :037 > p = DotHash::Properties.new({:foo => :bar})
=> #<DotHash::Properties:0x007f8cf4067740 @hash={:foo=>:bar}>
1.9.3-p125 :038 > p.respond_to?(:foo)
=> false
1.9.3-p125 :039 > p.foo
=> :bar
(defn all-functions
([root-n] (all-functions root-n []))
([cur fns]
(if (= java.util.ArrayList (class cur))
(if (empty? cur)
fns
(map #(all-functions % fns) cur))
(if (= org.jruby.ast.DefnNode (class cur))
(all-functions (.childNodes cur) (conj fns cur))
(all-functions (.childNodes cur) fns)))))
@jsl
jsl / gist:4547933
Created January 16, 2013 15:26
How to calculate the default password hash used by Authlogic
(defn sha-512
"Return the SHA-512 of the given string"
[data]
(let [md (. java.security.MessageDigest getInstance "SHA-512")]
(. md update (.getBytes data))
(let [bytes (. md digest)]
(reduce #(str %1 (format "%02x" %2)) "" bytes))))
(defn hash-repeatedly
"Hash the given string n number of times"
@jsl
jsl / clj-yaml-test.clj
Last active December 12, 2015 02:29
clj-yaml deserializes into arbitrary data types
;; Clojure library clj-yaml deserializes into arbitrary Java classes
user=> (require '[clj-yaml.core :as yaml])
nil
user=> (yaml/parse-string "!!java.io.File")
#<File >
@jsl
jsl / vmastat troubleshooting.txt
Last active December 12, 2015 08:29
vmstat example
vmstat is a helpful tool for figuring out performance problems on servers.
user@server-prod-db01:~$ vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 10860 51884 41656 3712392 0 0 1297 107 0 0 10 3 84 3
0 0 10860 50388 41668 3713204 0 0 41 446 1409 1173 6 2 91 1
The "b" column shows blocked processes waiting for IO. In this case it's 0, which is a good thing.
@jsl
jsl / strace resque
Created February 9, 2013 11:08
Figuring out what resque processes are doing
In the resque web interface, if you see a process that is hanging for a long
time, you can figure out what it's waiting on by looking at the PID. Say
we see pid 4894 taking a long time. In the console do:
$ sudo strace -p 4894
Process 4894 attached - interrupt to quit
wait4(15045,
The strace just hangs there, and we think that perhaps the process isn't
doing anything. If you look at the man page for wait4, you see that the
@jsl
jsl / rails_patch_3.2.12.sh
Created February 11, 2013 19:07
Script to patch recent Rails versions for CVE-2013-0269 and CVE-2013-0276.
#!/bin/bash
# First change your Rails dependency in the Gemfile to 3.2.12.
# Make sure afterwards that the commit includes the update to the Rails version
# and that it contains the JSON gem version 1.7.7.
git co -b patch_CVE-2013-0269_CVE-2013-0276
bundle update rails
bundle update json
git add -A
@jsl
jsl / gist:5166174
Created March 14, 2013 23:24
Debugging with jruby

JRuby debugging with 'step' and 'next'

If you run without the --debug flag, jruby steps into functions rather than skipping them. Perhaps that is related to this warning when starting with -rdebug and omitting the --debug flag:

warning: tracing (e.g. set_trace_func) will not capture all events without --debug flag

Running 'next' does seem to work properly when I run the jruby debugger as follows:

@jsl
jsl / gist:6123252
Created July 31, 2013 15:52
Zerigo DDNS update script in Haskell
import Text.XML.Light
import Network.Curl
import System.IO
import System.Environment
import System.Exit
extractIP :: String -> String
extractIP xmlString =
let
element = parseXMLDoc xmlString >>= findElement (QName "ipv4" Nothing Nothing)
{
"auto_complete_commit_on_tab": true,
"color_scheme": "Packages/Color Scheme - Default/Blackboard.tmTheme",
"draw_white_space": "all",
"ensure_newline_at_eof_on_save": true,
"font_size": 23.0,
"hot_exit": false,
"remember_open_files": false,
"rulers":
[