Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
keithrbennett / module-method-doesnt-override-class-instance-method.rb
Last active September 18, 2022 07:54
Shows that a module instance method will not overwrite a class instance method of the same name if `include` is used, but *will* if `prepend` is used.
#!/usr/bin/env ruby
module M
def foo
puts 'I am a module instance method.'
end
end
# Class whose foo instance method is defined *after* the include.
class C1
@keithrbennett
keithrbennett / force_class_inclusion_of_module.rb
Last active September 15, 2022 15:01
Illustrates forcing inclusion of a module by another included module in Ruby.
#!/usr/bin/env ruby
# From solution at https://stackoverflow.com/questions/73676857/guaranteeing-that-module-includer-classes-include-another-module-in-ruby.
module M1
def m1; puts 'M1 was included by M2.'; end
end
module M2
def self.included(including_class)
#!/usr/bin/env ruby
# Illustrates cycling through the SemanticLogger log levels with signals.
# For reference by https://github.com/reidmorrison/semantic_logger/issues/231.
require 'semantic_logger'
# Redefine add_signal_handler to output to STDERR (omit TTIN behavior for brevity):
module SemanticLogger
def self.add_signal_handler(log_level_signal = "USR2", thread_dump_signal = "TTIN", gc_log_microseconds = 100_000)
@keithrbennett
keithrbennett / trap_user_signals.rb
Created July 28, 2022 04:56
Illustrates the use of 'trap' in Ruby for crude interprocess communication
#!/usr/bin/env ruby
# Author: @keithrbennett (Github)
# Illustrates how to respond to Unix signals in a Ruby program, using SIGUSR1 and SIGUSR2
# for user-defined signals, and SIGINT for trapping Ctrl-C.
require 'awesome_print'
require 'json'
require 'yaml'
#!/usr/bin/env ruby
require 'nokogiri'
def process_example(message, xml_text, use_noblanks_option)
puts message
puts "XML text: #{xml_text.inspect}"
doc = Nokogiri::XML(xml_text) { |config| use_noblanks_option ? config.noblanks : config }
puts 'Resulting XML document:'
puts doc.inspect; puts; puts
@keithrbennett
keithrbennett / rfg-set-up-repos.rb
Last active September 23, 2021 00:50
Installs all repos needed for Ruby for Good 2021.
#!/usr/bin/env ruby
REPO_URLS = %w{
https://github.com/rubyforgood/casa
https://github.com/rubyforgood/cep-backend
https://github.com/rubyforgood/cep-ui
https://github.com/rubyforgood/circulate
https://github.com/rubyforgood/human-essentials
https://github.com/rubyforgood/shelter-assist
@keithrbennett
keithrbennett / module-class-state.rb
Last active April 13, 2021 20:17
Illustrates that Ruby modules *can* have state but only on the module level, not the instance level.
#!/usr/bin/env ruby
module M
class << self
attr_accessor :foo
end
end
class C1
def call
@keithrbennett
keithrbennett / gist:18f10124354d62eb8ba5feafaa9b39dc
Last active February 15, 2021 20:13
Simple Ractor test based on Koichi's example at https://bugs.ruby-lang.org/issues/17497
#!/usr/bin/env ruby
# Run in directory containing `compar.c`, e.g. from https://github.com/ruby/ruby/blob/master/compar.c.
require 'etc'
WORDS = Ractor.make_shareable File.readlines('/usr/share/dict/words').map(&:chomp).map(&:downcase).sort
TRY_COUNT = Etc.nprocessors
puts "Measuring first sequentially on main ractor and then with #{TRY_COUNT} ractors:\n\n"
@keithrbennett
keithrbennett / strip-zero-width-spaces.rb
Last active February 8, 2021 04:20
Removes Unicode "\u200B" zero width spaces; useful for copying source code from O'Reilly learning books
#!/usr/bin/env ruby
# strip-zero-width-spaces.rb
#
# Removes Unicode "\u200B" zero width spaces; useful for copying source code from O'Reilly learning books
#
# Usage:
#
# strip-zero-width-spaces.rb < original_file > new_stripped_file
@keithrbennett
keithrbennett / binary_search.rb
Created February 7, 2021 06:02
Binary search method in Ruby - < works better than <=, right?
#!/usr/bin/env ruby
def bsearch(array, search_value)
lower_bound = 0
upper_bound = array.length - 1
n = 0
while lower_bound < upper_bound
n += 1