This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Refinements | |
module DoFoo | |
refine Object do | |
def foo; :foo; end | |
end | |
end | |
module DoBar | |
refine Object do | |
def bar; :bar; end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def sum | |
reduce(0) { |sum, x| sum + x } | |
end | |
end | |
class Dice | |
def self.roll(sides = 6) | |
rand(sides) + 1 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.! |