Skip to content

Instantly share code, notes, and snippets.

Avatar
📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
View GitHub Profile
@baweaver
baweaver / DependencyInjectionInRuby.md
Last active May 2, 2021 — forked from blairanderson/DependencyInjectionInRuby.md
Dependency Injection in Ruby. Originally from Jim Weirich’s blog which does not exist except for googles cache. (I wanted headers, so fork it is)
View DependencyInjectionInRuby.md

Dependency Injection in Ruby 07 Oct 04

Introduction

At the 2004 Ruby Conference, Jamis Buck had the unenviable task to explain Dependency Injection to a bunch of Ruby developers. First of all, Dependency Injection (DI) and Inversion of Control (IoC) is hard to explain, the benefits are subtle and the dynamic nature of Ruby make those benefits even more marginal. Furthermore examples using DI/IoC are either too simple (and don’t convey the usefulness) or too complex (and difficult to explain in the space of an article or presentation). I once attempted to explain DI/IoC to a room of Java programmers (see onestepback.org/articles/dependencyinjection/), so I can’t pass up trying to explain it to Ruby developers.

Thanks goes to Jamis Buck (the author of the Copland DI/IoC framework) who took the time to review this article and provide feedback.

What is Dependency Injection?

@baweaver
baweaver / serializer_benchmarks.rb
Created Mar 19, 2021 — forked from aishfenton/serializer_benchmarks.rb
Performance comparison of different ruby serializer methods
View serializer_benchmarks.rb
# sudo gem install bson
# sudo gem install bson_ext
# sudo gem install yajl-ruby
# sudo gem install json
# sudo gem install msgpack
require 'rubygems'
require 'benchmark'
require 'yaml'
require 'bson'
View fizzbuzz_proper.rb
divisible_by = -> n, v { v % n == 0 }.curry
(1..100).each { |v|
puts case v
when divisible_by[15] then 'FizzBuzz'
when divisible_by[3] then 'Fizz'
when divisible_by[5] then 'Buzz'
else
v
end
View quick_grid_grouped_join.rb
grouped_join = -> join:, group_join:, group_count: 3 {
-> list {
list.each_slice(group_count).map { _1.join(join) }.join(group_join)
}
}
# 9x9 Game Board Grid
grid = [[*0..8]] * 9
cell_seperator = ' '
View enumerable_article_pre_release.md

Introduction

Enumerable. Debatably one of, if not the, most powerful features in Ruby. As a majority of your time in programming is dealing with collections of items it's no surprise how frequently you'll see it used.

Difficulty

Foundational

Some knowledge required of functions in Ruby. This post focuses on foundational and fundamental knowledge for Ruby programmers.

View current_progress_enumerable_article.md

Introduction

Enumerable. Debatably one of, if not the, most powerful features in Ruby. As a majority of your time in programming is dealing with collections of items it's no surprise how frequently you'll see it used.

Difficulty

Foundational

Some knowledge required of functions in Ruby. This post focuses on foundational and fundamental knowledge for Ruby programmers.

View roman_numeral.rb
def roman_numeral(n) = case n
in 1000..3000 then 'M' + roman_numeral(n - 1000)
in 900..999 then 'CM' + roman_numeral(n - 900)
in 500..899 then 'D' + roman_numeral(n - 500)
in 400..499 then 'CD' + roman_numeral(n - 400)
in 100..399 then 'C' + roman_numeral(n - 100)
in 90..99 then 'XC' + roman_numeral(n - 90)
in 50..89 then 'L' + roman_numeral(n - 50)
in 40..49 then 'XL' + roman_numeral(n - 40)
in 10..39 then 'X' + roman_numeral(n - 10)
View nokogiri_match.rb
# Pattern to match a link: <a/>
node in { name: 'a' }
# Pattern for a link with a class: <a class="foo"/>
node in { name: 'a', class: 'foo' }
# Pattern to select that link: <a class="foo">{.}</a>
node => { name: 'a', class: 'foo', children: }
# Pattern to select the target URL: <a class="foo" href="{.}"/>
@baweaver
baweaver / matchable_example.rb
Created Jan 31, 2021
Quick Pattern Matching interface demonstration
View matchable_example.rb
require 'matchable'
class Person
include Matchable
deconstruct :new
deconstruct_keys :name, :age
attr_reader :name, :age
@baweaver
baweaver / ruby_galaxy_poker_pattern_matching.rb
Created Jan 28, 2021
Variant of pattern matching example from RubyGalaxy talk
View ruby_galaxy_poker_pattern_matching.rb
class Card
SUITS = %w(S H D C).freeze
RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A).freeze
RANKS_SCORES = RANKS.each_with_index.to_h
include Comparable
attr_reader :suit, :rank
def initialize(suit, rank)