View hash_defaults.rb
## | |
# A standard Hash with default proc, which in this case sets the value to the key. | |
default_value_to_key = Hash.new { |hash, key| hash[key] = key } | |
default_value_to_key[:nope] | |
#=> :nope | |
## | |
# This is the same as the above proc with a Hash literal. | |
another_value_to_key = {} | |
another_value_to_key.default_proc = ->(hash, key) { hash[key] = key } |
View oh_dear.rb
changes = {IRB::Color::BLUE => IRB::Color::YELLOW} | |
IRB::Color.const_get(:TOKEN_SEQ_EXPRS).tap do |token_seq_exprs| | |
changes.each do |old_color, new_color| | |
token_seq_exprs.filter_map do |type, ((color, _), _)| | |
color == old_color && type | |
end.each do |matching_type| | |
token_seq_exprs.dig(matching_type, 0)[0] = new_color | |
end | |
end |
View fetch.rb
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'async' | |
require 'async/barrier' | |
require 'async/http/client' | |
require 'async/http/endpoint' | |
require 'async/logger' | |
require 'optionparser' |
View blocks.rb
## | |
# Examples without block arguments | |
def example_without_block(function) | |
function.call + 'ay' | |
end | |
example_without_block ->{ 'ok' } | |
#=> "okay" | |
def example_explicit_block(&block) | |
block.call + 'ay' |
View paint
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'json' | |
require 'optionparser' | |
KEYWORDS = %i[color metadata glow_in_the_dark coats].freeze | |
DEFAULTS = {coats: 3, glow_in_the_dark: true}.freeze |
View cistercian.rb
# frozen_string_literal: true | |
module Cistercian | |
LINE = %w[■ ■ ■].freeze | |
CENTER_DOT = ' ■ ' | |
GLYPHS = [ | |
[' ', | |
' ', | |
' '], | |
['■■■', |
View gross_weight.rb
class GrossWeight | |
include Comparable | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
def <=>(other) |
View async_fetch.rb
require 'async' | |
require 'async/barrier' | |
require 'open-uri' | |
def async_fetch(*urls) | |
barrier = Async::Barrier.new | |
Async do | |
urls.map do |url| | |
barrier.async { URI.open url, &:read } |
View .pryrc
Pry.config.prompt = Pry::Prompt[:simple] | |
Pry.config.pager = false | |
## | |
# Support refinements at top level in Pry. | |
Pry.hooks.add_hook :when_started, :context_switch do |_output, _binding, pry| | |
pry.binding_stack = [TOPLEVEL_BINDING] | |
end | |
## |
View to_struct.rb
module ToStruct | |
refine Hash do | |
def to_struct(class_name = nil) | |
if class_name | |
Object.const_set(class_name, Struct.new(*keys)) | |
Object.const_get(class_name).new(*values) | |
else | |
Struct.new(*keys).new(*values) | |
end | |
end |
NewerOlder