Skip to content

Instantly share code, notes, and snippets.

View baweaver's full-sized avatar
📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
View GitHub Profile
def method_name(*args)
in 1, 2, 'foo', *
# ...
in *, 'bar', *
# ...
in String, String, *
# ...
else
'default'
end
const ROOT = document.getElementById("root");
const PREFIX = '::';
const GRAY = '#CCC';
const KATEX_OPTIONS = {
throwOnError: false
};
function slicePrefix(str) {
return str.slice(PREFIX.length);
}
module DashingRefinement
refine Integer do
def -(b) "#{self}-#{b}" end
end
refine String do
def -(b) Date.parse("#{self}-#{b}") end
end
end
# 1 + 1
AstBuilder.build('1 + 1').to_ast.source
=> "1 + 1"
# From s-expressions
ast = AstBuilder.build { s(:send, s(:int, 1), :+, s(:int, 1)) }.to_ast
=> s(:send, s(:int, 1), :+, s(:int, 1))
# Current
ast.source
# Original Code
hash = { something: 'fun' }
method_called(with, some, hash)
# AST
(begin
(lvasgn :hash
(hash
(pair
(sym :something)
(def :route
(args
(arg :verb)
(arg :path)
(optarg :options
(hash))
(blockarg :block))
(begin
(...)
(lvasgn :signature
# Bad - Hard to read
some.really.long.chain_of_things(:with,
:arguments,
:that,
:are,
:out_here,
:is_unreadable,
:at_a_glance)
# Good - Eye doesn't flow far right then
@baweaver
baweaver / rubyconf_2019_raffle_winner.rb
Created December 17, 2019 06:52
My spectacularly convoluted selection criteria
names = %w(
@OtherChrisPine @DaveAronson @mellowfish @ktcarrel18 @yvonesre @molbrwn @_MylesHigh_ @DoodlingDev @nilsding @mae701
)
totally_random_results = "Lemur fun!"
.chars
.map(&:ord)
.zip(names.shuffle.map(&:ord))
.sample
.sum
Cons = Struct.new(:head, :tail)
case Cons[1, Cons[2]]
in Cons[head. tail] then [head, tail]
end
# => [1, #<struct Cons head=2, tail=nil>]
case Cons[1, Cons[2]]
in Cons[1, Cons[head, tail]] then [head, tail]
end
case { it: { works: { on: 1, nested: 'hashes' }, cool: true } }
in it: { works: { on: Integer, nested: String }, cool: TrueClass }
true
else
false
end
# => true
case [{ that: { includes: [:this, :too] }}, { more: 'splats'}, {see?: true}]
in [{ that: { includes: Array } }, *]