Skip to content

Instantly share code, notes, and snippets.

📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
Block or report user

Report or block baweaver

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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
View pattern_match_hash_array.rb
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 } }, *]
@baweaver
baweaver / pattern_matching.md
Created Nov 24, 2019
Current WIP of pattern matching documentation. Still needs more work.
View pattern_matching.md

Pattern Matching

Pattern match statements are an extension of the case expression. They introduce a new keyword to case statements: in.

The in expression

in is used in place of when to use pattern matching behaviors. These two syntaxes cannot be mixed.

An in expression is different from when in that it allows one to match against data structures:

@baweaver
baweaver / symbol_ap.rb
Created Oct 19, 2019
Stealing some vague ideas from Applicatives in Haskell, probably wrongly but it amuses me so...
View symbol_ap.rb
class Symbol
def ap(*applied_args)
-> *block_args {
if applied_args.empty?
receiver, *new_args = block_args[0]
receiver.send(self, *new_args)
else
self.to_proc.call(*block_args, *applied_args)
end
}
@baweaver
baweaver / favorite_things_ruby_edition
Last active Sep 21, 2019
A Few of my Favorite Things translated to Ruby (WIP)
View favorite_things_ruby_edition
Ruby programming
And Gems pushed to GitHub
Cloud based deployments and CI / CD
Tests that will pass on the first try
These are a few of my favorite things
Just Rails new app name
and omakase thinking
convention o'er configuration
and sane default settings
You can’t perform that action at this time.