Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@havenwood
havenwood / universe.rb
Created February 18, 2024 00:45
An example of how to create a custom `new` method for all Classes or just a particular Class
class Class
def new(...)
warn 'Overriding Class#new'
instance = allocate
instance.send(:initialize, ...)
instance
end
end
Method Saves to DB Default Accessor Validations Callbacks Touches updated_at Readonly check
attribute=
write_attribute
update_attribute
attributes=
update
update_column
update_columns
@havenwood
havenwood / kernel.rb
Last active January 22, 2024 00:01
A spike implementing part of this nifty Ruby feature request: https://bugs.ruby-lang.org/issues/20196
# frozen_string_literal: true
module Kernel
def Binary(string, exception: true)
hex = string.b
hex.gsub!(/#[^#]*$\R*/, '')
hex.gsub!(/"[^"]*"/) do |quotes|
quotes[1..-2].unpack1('H*')
end
hex.delete!("\s\t")
@havenwood
havenwood / struct_literal.rb
Last active January 11, 2024 23:52
An anonymous Struct in pure Ruby (hoping Ruby adds a real one)
module StructLiteral
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
@havenwood
havenwood / ring_buffer.rb
Created July 5, 2023 19:01
A ring buffer implementation example for Ruby Discord.
# frozen_string_literal: true
class RingBuffer
Nothing = Data.define
EMPTY = Nothing.new
attr_reader :size
def initialize(capacity:)
@capacity = capacity
@havenwood
havenwood / color_registry.rb
Created July 3, 2023 17:35
Example of a Curses colors registry Singleton for IRC
# frozen_string_literal: true
require 'curses'
require 'singleton'
module Isene
class ColorRegistry
include Singleton
attr_reader :colors
@havenwood
havenwood / chruby_local.sh
Last active June 21, 2023 07:03
Install the latest stable Ruby as a local user (non-root).
##
# A non-root installation of the latest Ruby with chruby
# Install chruby (https://github.com/postmodern/chruby#readme)
mkdir -p $HOME/src
cd $HOME/src
wget -O chruby-0.3.9.tar.gz https://github.com/postmodern/chruby/archive/v0.3.9.tar.gz
tar -xzvf chruby-0.3.9.tar.gz
cd chruby-0.3.9/
PREFIX=$HOME/.chruby make install
@havenwood
havenwood / inline_runner.rb
Created September 28, 2022 20:32
A little script to run RuboCop on a Ruby String from within Ruby
require 'rubocop'
require 'tempfile'
class InlineRunner
DEFAULT = RuboCop::Runner.new({}, RuboCop::ConfigStore.new)
def initialize(runner: DEFAULT, filename: 'runner.rb')
@runner = runner
@file = Tempfile.new(filename)
end
@havenwood
havenwood / rescue.rb
Created September 7, 2012 22:02
devnull_
require 'nokogiri'
url = 'http://zales.1.ai'
Nokogiri::HTML(`curl #{url}`).css('title').text
@havenwood
havenwood / super.rb
Created May 25, 2018 17:54
Example's of how Ruby's super keyword passes on args and blocks
class Foo
def foo arg = false
{arg: arg, block: block_given?}
end
end
class Bar < Foo
def foo *args
super
end