Skip to content

Instantly share code, notes, and snippets.

@benmiles
Created November 9, 2012 21:29
Show Gist options
  • Save benmiles/4048378 to your computer and use it in GitHub Desktop.
Save benmiles/4048378 to your computer and use it in GitHub Desktop.

Clojure for Rubyists

Philosophy

Rich Hickey: "you are able to write only code that matters."

Comments

Use paredit.

Use a decent editing env (repl built-in)

ClojureDocs

Destructuring

def myfunc(foo, options={})
  bar = options[:bar]
  baz = options[:baz]

  # do something 
end
(defn myfunc [foo {:keys [bar baz]}]
  ;; do something
)

Destructuring (part II)

def awesome()
end
(defn awesome []
)

Open a json file, parse it & return a hash

File.open("somefile.json") do |f|
  json = f.read
  ActiveSupport::JSON.decode(json)
end
(-> "somefile.json"
    slurp
    cheshire.core/parse)

Get value from a nested hash (safely)

if h && h[:a] && h[:b] && h[:c]
  h[:a][:b][:c]
end

Get value from a nested hash (safely)

if h && h[:a] && h[:b] && h[:c]
  h[:a][:b][:c]
end
(get-in h :a :b :c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment