This file contains 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 Object | |
def constants_hash | |
constants = self.constants | |
constant_values = constants.map do |const| | |
self.const_get const | |
end | |
constants.zip(constant_values).to_h | |
end | |
end |
This file contains 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
require 'bundler' | |
class Gemfile | |
def initialize gemfile_path = 'Gemfile' | |
@bundler = Bundler::Dsl.new | |
@bundler.eval_gemfile gemfile_path | |
end | |
def gems group | |
@bundler.dependencies.select do |gem| |
This file contains 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
require 'stringio' | |
module Capture | |
def self.stdout &block | |
$stdout = captured_stdout = StringIO.new | |
block.call | |
captured_stdout | |
ensure | |
$stdout = STDOUT | |
block.call |
This file contains 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
require 'io/console' | |
ARROWS = {"[A"=>:up, "[B"=>:down, "[C"=>:right, "[D"=>:left} | |
char = STDIN.getch | |
ARROWS[STDIN.read_nonblock(2)] if char == "\e" |
This file contains 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 Weazel | |
CHARS = [*'A'..'Z', ' '] | |
def initialize target | |
@target = target | |
@best = Array.new(28) { CHARS.sample }.join | |
end | |
def call | |
0.upto Float::INFINITY do |n| |
This file contains 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 RockPaperScissors | |
OPTIONS = [:rock, :paper, :scissors] | |
include Comparable | |
attr_reader :hand | |
def initialize | |
@hand = OPTIONS.sample | |
end |
This file contains 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
require 'equalizer' | |
Point = Struct.new(:x, :y) { include Equalizer.new(*members) } | |
Point.new(0, 0) | |
#=> #<struct Point x=0, y=0> | |
Point.new(0, 0) == Point.new(0, 0) | |
#=> true |
This file contains 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
require 'equalizer' | |
class EqualizableStruct < Struct | |
def initialize *args, &block | |
class << self | |
include Equalizer.new(*members) | |
end | |
super | |
end |
This file contains 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 Future | |
def initialize &work | |
@worker = Thread.new &work | |
end | |
def ready? | |
@worker.alive? | |
end | |
def value |
This file contains 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
require_relative 'tuplespace' | |
DRb.start_service | |
space = TupleSpace.new tuple_space: DRbObject.new(nil, 'druby://0.0.0.0:3000') |