Skip to content

Instantly share code, notes, and snippets.

View madstap's full-sized avatar

Aleksander Madland Stapnes madstap

View GitHub Profile
@madstap
madstap / Quiz 2.1.rb
Last active August 29, 2015 14:17
Quiz for Tealeaf course 2 lesson 1
# Quiz: Lesson 1
# Quiz from lesson one materials. You can do this yourself, or create a new gist
# or blog entry, answer the quiz, and post the link to the forums and others can
# take a look.
# 1. Why do they call it a relational database?
# Because it stores data and the relations between the data.
@madstap
madstap / Quiz 2.2.rb
Last active August 29, 2015 14:17
Quiz for Tealeaf course 2 lesson 2
# Quiz: Lesson 2
# Some exercises from lesson 2 materials:
# 1.Name all the 7 (or 8) routes exposed by the resources keyword in the
# routes.rb file. Also name the 4 named routes, and how the request is routed to
# the controller/action.
# /app/config/routes.rb
resources :posts
@madstap
madstap / Quiz 2.3.md
Last active August 29, 2015 14:19
Quiz for Tealeaf course 2 lesson 3

Quiz: Lesson 3

1 What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates?

Rendering displays the view to be rendered on the same action, instance variables set up will be available in this view. Redirecting makes a new http request and will hit another controller action forgetting any instance variables.

2 If I need to display a message on the view template, and I'm redirecting, what's the easiest way to accomplish this?

To use the flash.

@madstap
madstap / gist:39f4589d5ae40f51f21c
Created February 2, 2016 15:55
Cider stacktrace
java.lang.Exception: Unexpected EOF. [at line 16, column 1]
at mranderson046.rewrite_clj.v0v4v13_20150917v134033_1.rewrite_clj.reader$throw_reader.doInvoke (reader.clj:18)
clojure.lang.RestFn.invoke (RestFn.java:425)
mranderson046.rewrite_clj.v0v4v13_20150917v134033_1.rewrite_clj.parser.core$eval19602$fn__19603.invoke (core.clj:78)
clojure.lang.MultiFn.invoke (MultiFn.java:229)
mranderson046.rewrite_clj.v0v4v13_20150917v134033_1.rewrite_clj.reader$read_with_meta.invoke (reader.clj:122)
mranderson046.rewrite_clj.v0v4v13_20150917v134033_1.rewrite_clj.parser.core$parse_next.invoke (core.clj:34)
mranderson046.rewrite_clj.v0v4v13_20150917v134033_1.rewrite_clj.parser.core$parse_delim$fn__19584.invoke (core.clj:42)
mranderson046.rewrite_clj.v0v4v13_20150917v134033_1.rewrite_clj.reader$read_repeatedly$fn__19483.invoke (reader.clj:131)
clojure.core$repeatedly$fn__5111.invoke (core.clj:4921)
;; The code starts as a string in a text file, like any programming language.
"(defn f [x]
(* x x))"
;; Then it's read by the reader.
(with-in-str
"(defn f [x]
(* x x))" (read))
;; Read returns this data structure:
(ns things.defn-spec
(:require [clojure.spec :as s]
[clojure.test :as test :refer [deftest testing is are]]
[clojure.spec.test :as stest]))
'([name doc-string? attr-map? [params*] prepost-map? body]
[name doc-string? attr-map? ([params*] prepost-map? body) + attr-map?]) ; What's this last map for?
(defn non-&-sym? [x] (and (symbol? x) (not= '& x)))
;; Eval this in *scratch* (or emacs setup)
(setq cider-cljs-lein-repl
"(do (require 'figwheel-sidecar.repl-api)
(figwheel-sidecar.repl-api/start-figwheel!)
(figwheel-sidecar.repl-api/cljs-repl))")
;; Then m-x cider-jack-in-clojurescript
@madstap
madstap / conform-let.clj
Last active October 28, 2016 21:02
let with conforming bindings
(ns conform-let
(:require
[clojure.spec :as s]
[clojure.core.specs :as core-specs]))
(s/def ::conform-bindings
(s/and vector?
(s/spec (s/* (s/cat :binding ::core-specs/binding-form
:spec some?
@madstap
madstap / enlive_parse_unparse.clj
Created January 15, 2017 08:00
I always forget which fns are parse and unparse in enlive
(ns foo.core
(:require
[net.cgrand.enlive-html :as e]))
(def x "<p>asd</p><a href=\"google.com\">google</a>")
;; Parse
(def y (e/html-snippet x))
y ;=> ({:tag :p, :attrs nil, :content ("asd")} {:tag :a, :attrs {:href "google.com"}, :content ("google")})
@madstap
madstap / ampersand.cljs
Created January 16, 2017 07:57
Clojurescript &
;; I can use & as a name with def
(def & identity)
& ;=> #object[Function "function (a){return a}"]
;; But if I do
(& 42)
;; It throws the exception.