Skip to content

Instantly share code, notes, and snippets.

@ghoseb
ghoseb / ns-cheatsheet.clj
Last active April 11, 2024 05:28 — forked from alandipert/ns-cheatsheet.clj
Clojure ns syntax cheat-sheet
;;
;; NS CHEATSHEET
;;
;; * :require makes functions available with a namespace prefix
;; and optionally can refer functions to the current ns.
;;
;; * :import refers Java classes to the current namespace.
;;
;; * :refer-clojure affects availability of built-in (clojure.core)
;; functions.
(require '[clj-serializer.core :as serializer])
(require '[clj-json.core :as m-json])
(require '[org.danlarkin.json :as dl-json])
(require '[clojure.contrib.json.read :as cc-json-read])
(require '[clojure.contrib.json.write :as cc-json-write])
(defn nano-time []
(System/nanoTime))
(defn timed [task]
#!/usr/bin/env ruby
def find_deps(cookbook_dir)
nel = Hash.new { |h, k| h[k] = [] }
Dir.glob("#{cookbook_dir}/*/").each do |r|
deps_for(r, nel)
end
nel
end
# In your test_helper.rb
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
(ns org.dipert.reconfig
"Reads a file of Clojure data into *config*
when a SIGHUP is recieved.
Useful for daemons that support the reloading of
configuration data while running."
(:use [clojure.contrib.io :only (reader)]
[clojure.contrib.logging :only (info warn)])
(:import (sun.misc Signal SignalHandler)
(java.io File PushbackReader)))
@mnutt
mnutt / Instrument Anything in Rails 3.md
Created September 6, 2010 06:50
How to use Rails 3.0's new notification system to inject custom log events

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

(defn mapmap
"Return a new map, mapping keys to (keyfn key) and mapping values to
(valfn val)"
([valfn map]
(mapmap identity valfn map))
([keyfn valfn map]
(persistent! (reduce
(fn [c [k v]] (assoc! c (keyfn k) (valfn v)))
(transient {})
map))))
# Allow RSpec assertions over the elements of a collection. For example:
#
# collection.should all_be > 0
#
# will specify that each element of the collection should be greater
# than zero. Each element of the collection that fails the test will
# be reported in the error message.
#
# Examples:
# [1,1,1].should all_be eq(1)
@pjb3
pjb3 / method_vs_attr_reader.rb
Created October 16, 2010 11:56
Apparently attr_reader is faster than a method in all ruby implementations. Why?
require 'benchmark'
puts
puts ENV["RUBY_VERSION"]
class A
def initialize(foo)
@foo = foo
end
def foo
class A
def implicit_block_implicit_ensure
begin
"block"
ensure
"ensure"
end
end
def implicit_block_explicit_ensure