Skip to content

Instantly share code, notes, and snippets.

Avatar
📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
View GitHub Profile
View live_ast_quick_ref.rb
# Ideal syntax for pattern matched methods:
def handle_response(body)
in 200, response
# Ok response
in 500, error
# Handle failure
else
# No idea?
end
View pattern_match_method.rb
def method_name(*args)
in 1, 2, 'foo', *
# ...
in *, 'bar', *
# ...
in String, String, *
# ...
else
'default'
end
View katex_labeled_sub.js
const ROOT = document.getElementById("root");
const PREFIX = '::';
const GRAY = '#CCC';
const KATEX_OPTIONS = {
throwOnError: false
};
function slicePrefix(str) {
return str.slice(PREFIX.length);
}
View dashingly.rb
module DashingRefinement
refine Integer do
def -(b) "#{self}-#{b}" end
end
refine String do
def -(b) Date.parse("#{self}-#{b}") end
end
end
View ast_source.rb
# 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
View hash_lvar.rb
# Original Code
hash = { something: 'fun' }
method_called(with, some, hash)
# AST
(begin
(lvasgn :hash
(hash
(pair
(sym :something)
View route_hash_args_ast.rb
(def :route
(args
(arg :verb)
(arg :path)
(optarg :options
(hash))
(blockarg :block))
(begin
(...)
(lvasgn :signature
View hanging_indentation.rb
# 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 Dec 17, 2019
My spectacularly convoluted selection criteria
View rubyconf_2019_raffle_winner.rb
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
View pattern_matching_cons.rb
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
You can’t perform that action at this time.