Skip to content

Instantly share code, notes, and snippets.

@malakai97
malakai97 / treatise.md
Last active March 22, 2024 20:46
Modules in Ruby

Modules in Ruby

On Composition

There has been sentiment that ruby's modules enable composition, but this is not the case. Composition describes a "has-a" relationship, whereas (in ruby) when a class includes a module, it creates an is-a relationship.

Forms/Patterns

There are four basic forms of modules in ruby, some good, some not so good.

@malakai97
malakai97 / elsewhere.rb
Last active April 23, 2019 17:25
Ruby #handles? style factories
Superclass.for(obj) # => The concrete subclass instance you wanted
@malakai97
malakai97 / DependencyInjectionInRuby.md
Created February 7, 2018 00:34 — forked from blairanderson/DependencyInjectionInRuby.md
Dependency Injection in Ruby. Originally from Jim Weirich’s blog which does not exist except for googles cache.

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?

@malakai97
malakai97 / cli.rb
Last active October 24, 2017 16:52
Simple configuration for ruby gems and apps
# lib/mygem/cli.rb
require "mygem"
require "mygem/configuration"
module MyGem
class CLI
def actual_cli_method(x,y)
MyGem.config = configuration(opts)
MyGem::StuffDoer.new(x).go(y)
@malakai97
malakai97 / yaml_parse_no_times.rb
Last active January 12, 2017 18:11
Ruby: Parse yaml without converting time-strings to objects
require 'psych'
class WorseScanner < Psych::ScalarScanner
def parse_time(s)
s
end
end
s = "---\nf: 2001-02-29T01:02:03-03:00" # Note this day doesn't exist.