Skip to content

Instantly share code, notes, and snippets.

View wconrad's full-sized avatar

Wayne Conrad wconrad

View GitHub Profile
@wconrad
wconrad / .ruby-version
Last active July 9, 2021 13:32
Demonstrate alleged bug in the faraday gem, wherein streaming appears to be broken by persistence
ruby-3.0.1
@wconrad
wconrad / si_byte_count.rb
Created May 28, 2019 20:03
Convert byte counts to SI units
class SiByteCount
def initialize(n)
@n = n
end
def to_s
n = signif(@n, 3)
UNITS.each do |coef, abbrev|
if n >= coef
@wconrad
wconrad / greetings.rb
Created December 20, 2018 21:27
OOP greetings example for Mirv in Ruby SO chat
class TimeRange
def initialize(start_hour, stop_hour)
@start_hour = start_hour
@stop_hour = stop_hour
end
def complete?
@start_hour && @stop_hour
end
@wconrad
wconrad / output.txt
Created June 14, 2017 18:14
Monkey-patch enumerator to call a progress object when iterating
1
"increment"
2
"increment"
3
"increment"
@wconrad
wconrad / method_missing_example.rb
Last active May 11, 2017 16:35
Method missing example
# Exmaple using method_missing to expose dynamic attributes.
class Foo
def initialize
@attrs = {:a => 1, :b => 2}
end
def method_missing(method, *args, &block)
if @attrs.has_key?(method.to_sym)
send(:[], method, *args)
@wconrad
wconrad / wrap_array.rb
Created May 5, 2017 15:31
How to wrap an array in Ruby
require "forwardable"
# How to wrap an array
class Foo
extend Forwardable
def_delegator :@a, :each
include Enumerable
@wconrad
wconrad / all.rb
Created February 10, 2017 15:28
Example of using polymorphism to replace switch statement
# frozen_string_literal: true
module PercyDbMigrate
module PrereqPolicy
# A prereq policy that migrates both selected classes and
# prerequesites.
class All
# Return the migration classes to run.
@wconrad
wconrad / user.rb
Created November 15, 2016 17:24
Some opaque logic
# Avoid linking an active user to franchises for which the user
# is inactive.
#
# * If the source user is inactive in all franchises, the user
# should be linked to all of the franchises in which it was
# present.
#
# * If the source user is active in all franchises, the
# destination user should be linked to all of the franchises
# in which it was present.
@wconrad
wconrad / home.feature
Created October 14, 2016 20:14
Proposed Cucumber feature for testing web app, using high-level steps rather than predefined "click here, fill in that" steps.
Feature: Home Page
As a user
I want a home page
So that I won't be homeless
Scenario: Shows the home page after logging in
Given that I am logged in as an ordinary user
Then I should be on the home page
@wconrad
wconrad / angle.rb
Last active September 18, 2016 02:15
Ruby class to encapsulate an angle
module CpuSim::Cpu
class Angle
def self.zero
new(0)
end
def self.radians(v)
new(v)