Skip to content

Instantly share code, notes, and snippets.


Keybase proof

I hereby claim:

  • I am dnagir on github.
  • I am dnagir ( on keybase.
  • I have a public key ASBRKxPaRBzw3Jtnf172oWrOYt26nFkIkKk1fQBviIFkHwo

To claim this, I am signing this object:

View dry_validation_and_struct.rb
require 'dry/validation'
require 'dry/types'
require 'dry/struct'
module Types
include Dry::Types()
class OrderContract < Dry::Validation::Contract
params do
dnagir / example.rb
Last active May 3, 2019
Dry Transaction Inheritance Repro
View example.rb
require 'bundler/inline'
gemfile do
gem 'dry-validation', '=1.0.0.rc1'
gem 'dry-transaction', '=0.13.0'
gem 'minitest', '=5.11.3'
require 'dry-validation'
require 'dry/transaction'
dnagir / maybe.rb
Last active Oct 18, 2017
Minimal Ruby Maybe monad
View maybe.rb
require 'singleton'
module Maybe
class Just < BasicObject
attr_reader :value
def initialize(value)
@value = value
dnagir / example.rb
Created Apr 19, 2016
ActiveRecord time truncation
View example.rb
gem 'activerecord', '4.2.5'
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.logger =
:adapter => 'postgresql',
:database => 'x'
dnagir / application_helper.rb
Created Aug 18, 2015
Render a different variant in rails
View application_helper.rb
module ApplicationHelper
def with_variant(new_variant, &block)
old_variants = lookup_context.variants
lookup_context.variants = [new_variant]
lookup_context.variants = old_variants
View shared_subject.rb
# spec/support/shared_subject.rb
module RSpecSharedSubjectMacros
# Usage:
# 1. Replace 'subject' with 'shared_subject' to share the first result acroos the whole context
# 2. (optional) replace 'before(:each)' blocks with 'shared_setup'
# 3. Done. The subject will be shared within one, single context (nested will be re-setup again)
# DANGER NOTE: Make sure you accept the disadvantages of this, such as:
dnagir /
Created Jun 25, 2014
Unsafe Ruby vars
> ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
> ruby test.rb
Run options: --seed 22679
# Running:
Finished in 2.320485s, 0.8619 runs/s, 172.3778 assertions/s.
dnagir /
Last active Aug 29, 2015
Chronic time_class (how come it doesn't break in Rubinius?)
> rvm use ruby-2.1.1
Using /Users/dnagir/.rvm/gems/ruby-2.1.1
> ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
> ruby test.rb
Run options: --seed 49028
# Running:
dnagir / eample.rb
Last active Aug 29, 2015
nil, nil, nil
View eample.rb
# In the context of Rails app (mailers)
# Somewhere in email helper
def from_address_by(person_name)
return nil unless person_name
address = # ex: ""
address.display_name = person_name # ex: "John Doe"
address.format # returns "John Doe <>"