View class_and_module_methods.rb
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
def modules_including_module(target_module) | |
ObjectSpace.each_object(Module).select do |object| | |
# All Class'es are modules, but we are not interested in them, so we exclude them. | |
!object.is_a?(Class) \ | |
&& \ | |
object.ancestors.include?(target_module) \ | |
&& \ | |
!object.equal?(target_module) | |
end | |
end |
View modules_needing_included_method.rb
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
# Any module (not class) that includes SemanticLogger::Loggable must also implement an | |
# `included` method that gets the including class to also include SemanticLogger::Loggable. | |
# This is because the module's instance method `logger`, when it becomes part of the including | |
# class, will look for `self.class.logger` when called, and `self` will evaluate to the instance | |
# of the class and not the module. Having the class include `Loggable` will cause a class method | |
# `logger` to be created so that this will work. | |
# | |
# This method returns the modules that have included Loggable but do not have an `included` | |
# method implementation. This is an imperfect test since we are not testing the content of that | |
# implementation, but I don't know a better way. |
View module-method-doesnt-override-class-instance-method.rb
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
#!/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 |
View force_class_inclusion_of_module.rb
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
#!/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) |
View semantic_logger_trap.rb
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
#!/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) |
View trap_user_signals.rb
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
#!/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' |
View xml.rb
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
#!/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 |
View rfg-set-up-repos.rb
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
#!/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 |
View module-class-state.rb
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
#!/usr/bin/env ruby | |
module M | |
class << self | |
attr_accessor :foo | |
end | |
end | |
class C1 | |
def call |
View gist:18f10124354d62eb8ba5feafaa9b39dc
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
#!/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" |
NewerOlder