Skip to content

Instantly share code, notes, and snippets.

View twe4ked's full-sized avatar
🐦
just setting up my twttr

Odin twe4ked

🐦
just setting up my twttr
View GitHub Profile
@janko
janko / 01-activerecord.rb
Created May 27, 2015 22:50
PostgreSQL JSON querying in Sequel (my presentation from our local Ruby meetup)
require "active_record"
ActiveRecord::Base.establish_connection('postgres:///testing')
ActiveRecord::Migration.verbose = false
ActiveRecord::Migration.class_eval do
create_table :played_quizzes, force: true do |t|
t.integer :player_ids, array: true
t.json :quiz_snapshot
end
@jasoncodes
jasoncodes / missing_foreign_key_constraints_spec.sql
Created September 1, 2014 04:51
Find missing foreign key constraints
SELECT CONCAT(col.table_name, '.', col.column_name)
FROM information_schema.columns col
INNER JOIN pg_catalog.pg_tables tbl ON tbl.schemaname = col.table_schema AND tbl.tablename = col.table_name
LEFT JOIN information_schema.columns col_type ON (
col.table_schema = col_type.table_schema AND
col.table_name = col_type.table_name AND
regexp_replace(col.column_name, '_id$', '_type') = col_type.column_name
)
LEFT JOIN (
(
@jashkenas
jashkenas / semantic-pedantic.md
Last active June 19, 2025 18:41
Why Semantic Versioning Isn't

Spurred by recent events (https://news.ycombinator.com/item?id=8244700), this is a quick set of jotted-down thoughts about the state of "Semantic" Versioning, and why we should be fighting the good fight against it.

For a long time in the history of software, version numbers indicated the relative progress and change in a given piece of software. A major release (1.x.x) was major, a minor release (x.1.x) was minor, and a patch release was just a small patch. You could evaluate a given piece of software by name + version, and get a feeling for how far away version 2.0.1 was from version 2.8.0.

But Semantic Versioning (henceforth, SemVer), as specified at http://semver.org/, changes this to prioritize a mechanistic understanding of a codebase over a human one. Any "breaking" change to the software must be accompanied with a new major version number. It's alright for robots, but bad for us.

SemVer tries to compress a huge amount of information — the nature of the change, the percentage of users that wil

operator infix !|| { associativity right precedence 100 } // associativity/precedence copied from ternary ? :
@infix func !||<T>(lhs:T?, rhs:@auto_closure ()->T) -> T {
switch lhs {
case nil: return rhs()
case _: return lhs!
}
}
@lilyball
lilyball / !||.swift
Last active August 29, 2015 14:04
`x !|| y` operator to provide the equivalent of `x ? x! : y`
operator infix !|| {
associativity right
precedence 100
}
func !||<T>(left: T?, right: @auto_closure () -> T) -> T {
if let val = left {
return val
}
return right()
@sidonath
sidonath / flash.rb
Last active August 29, 2015 14:00
An implementation of flash feature on top of Lotus::Action::Session
# A basic Rails-like Flash implementation built upon Lotus::Action::Session.
# Based upon Rack-flash:
# https://github.com/nakajima/rack-flash
module Flash
def self.included(action)
action.class_eval do
# We rely on features provided by Lotus::Action::Session so let's include
# it right here
include Lotus::Action::Session
@chendo
chendo / required_keyword_arguments.rb
Created March 28, 2014 03:19
My take on required keyword arguments in Ruby 2.0. This solution throws a nice backtrace, but still requires a name to be passed in.
module RequiredKeywordArguments
def required!(name)
backtrace = caller_locations(1).map { |c| c.to_s }
ex = ArgumentError.new("Missing required keyword argument '#{name}'")
ex.set_backtrace(backtrace)
raise ex
end
end
class Foo
if [ $EUID != 0 ]; then
echo "It's a weird tree."
else
echo ' _ __'
echo ' / `\ (~._ ./ )'
echo ' \__/ __`-_\__/ ./'
echo ' _ \ \/ \ \ |_ __'
echo ' ( ) \__/ -^ \ / \'
echo ' \_/ " \ | o o |.. / __'
echo " \\. --' ==== / || / \\ "
@henrik
henrik / ruby_2.1_experiment.rb
Last active January 4, 2016 09:49
Toying with Ruby 2.1 methods returning symbols.
module Wrapping
def wrap(method_name, &block)
# Alternative implementations: either will do the trick.
wrap_with_prepend(method_name, &block)
#wrap_with_bind(method_name, &block)
end
private
def wrap_with_prepend(method_name)
@joakimk
joakimk / doc.md
Last active December 25, 2015 03:59
Using rails constant missing features (ActiveSupport::Dependencies) outside of rails

Using ActiveSupport::Dependencies outside of rails seems just as fast as doing manual requires. I've found this out by replacing hundreds of requires in a unit tested code base with ActiveSupport::Dependencies without any change to the time it takes to run the test suite.

Using ActiveSupport::Dependencies also allows you to have circular depedencies like class Foo::Bar in foo/bar.rb and class Foo in foo.rb that points to Foo::Bar in it's class definition (like in a rails validation).

The classical way of solving this without ActiveSupport::Dependencies is to do:

# foo/bar.rb
class Foo
 class Bar