Skip to content

Instantly share code, notes, and snippets.

@saraid
saraid / combining_refinements.rb
Created February 8, 2019 23:37
how to combine refinements
module Refinements
module DoFoo
refine Object do
def foo; :foo; end
end
end
module DoBar
refine Object do
def bar; :bar; end
@saraid
saraid / dice.rb
Last active August 29, 2015 14:22
Quick dice roller script.
class Array
def sum
reduce(0) { |sum, x| sum + x }
end
end
class Dice
def self.roll(sides = 6)
rand(sides) + 1
end
class Class
def method_invert(hash_or_existing, inverted = nil)
hash_or_existing = Hash[[[hash_or_existing, inverted]]] unless hash_or_existing.respond_to? :each_pair
hash_or_existing.each_pair do |existing, inverted|
define_method(inverted) do |*args, &block|
if self.respond_to?(existing)
self.send(existing, *args, &block)
else
self.instance_variable_get(existing)
end.!