Skip to content

Instantly share code, notes, and snippets.

@the-undefined
the-undefined / ejixir.exs
Created May 27, 2017 15:36
Ejixir - reimplementing the Elixir `tl` and `hd` functions
defmodule Ejixir do
def j_tl(coll) do
[ _h | t ] = coll
t
end
def j_hd(coll) do
[ h | _t ] = coll
h
end
@the-undefined
the-undefined / Increment.elm
Created August 7, 2016 08:54
Tiny implementation for incrementing a counter
import Html exposing (div, text)
import Html.App as App
import Html.Events exposing (onClick)
type Msg = Increment
main : Program Never
main =
App.beginnerProgram
{ model = 0
@the-undefined
the-undefined / dom_helper.rb
Last active May 6, 2016 10:56
Capybara convenience module for finding elements by `data-X` values
# frozen_string_literal: true
module DomHelper
# Executes the given block within the context of a specific node identified
# using its data-xxx attributes. It will take any number of arguments.
#
# eg:
# user = create_user()
# within_element(id: user.id) do
# click_on 'Edit'
# end
@the-undefined
the-undefined / .gitconfig
Created March 16, 2016 15:01
Customize your git commit comment
[commit]
template = ~/.gitmessage
@the-undefined
the-undefined / char_combinations.rb
Created February 24, 2016 07:39
All of the possible combinations of letters in a string
def char_combinations(s)
length = s.size
list_comp = Array.new(length) do |start_index|
length.times.inject([]) do |combinations, end_index|
sub_str = s[start_index..end_index]
sub_str.size > 0 ? combinations << sub_str : combinations
end
end.flatten
end
@the-undefined
the-undefined / date_range.rb
Last active February 22, 2016 10:24
Generating a range of sequential dates
require 'date'
# OK: but the bracket faries are in abundence :()
((Date.today)..(Date.today + 7)).to_a
# BAD: for performance
# https://github.com/bbatsov/rubocop/blob/58761c6bbb0ba68f78651df672b93f9c15e1213f/lib/rubocop/cop/performance/times_map.rb
7.times.map { |n| Date.today + n }
# GOOD: concise, clear & performant
# Example of a guard clause performing a nil check
class BlahSpeaker
has_one :audience
def speak
return if audience.nil?
audience.address('blah ' * 100)
end
end
@the-undefined
the-undefined / year_query.rb
Created January 26, 2016 09:20
Make a query on the year of a date column (rails / PostgreSQL)
TimeWindow.where(%{EXTRACT(YEAR FROM start_date) = ?}, 2020)
@the-undefined
the-undefined / irb.sh
Last active January 21, 2016 09:12
Initialize is a private method, so.... put it under private
$ irb
001:0> Class.new.initialize
NoMethodError: private method `initialize' called for #<Class:0x007fc9188e3f08>
@the-undefined
the-undefined / presenter.rb
Last active January 20, 2016 15:28
Presenter that yield to the view for route information
class Presenter
include SortableColumns
def sortable_column_heading(text:, attr:, &block)
label = sort_link_text(text, attr)
link_params = sort_link_params(attr)
block.call(label, link_params)
end