Skip to content

Instantly share code, notes, and snippets.

@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
@keithrbennett
keithrbennett / test_iteration_constructs.rb
Last active February 1, 2021 20:24
Illustrates differences in behavior between (until, for) and (loop, map, upto).
#!/usr/bin/env ruby
# --------------------------------------------------------------------------------
# test_iteration_constructs.rb
#
# Illustrates differences in behavior of some iteration constructs.
# @keithrbennett, 2021-02-01
#
# Output is:
#
@keithrbennett
keithrbennett / git-project-url
Created January 30, 2021 18:52
Ruby script that outputs to stdout the project URL corresponding to a git remote URL
#!/usr/bin/env ruby
# git-project-url
#
# Outputs to stdout the project URL corresponding to a git remote URL.
#
# Example:
#
# (`open` is for Mac, use `xdg-open` on Linux, `start` on Windows)
# open $(git-project-url) # Opens 'origin' project page ('origin' is default)
@keithrbennett
keithrbennett / my-ractor.rb
Last active January 18, 2021 22:35
Illustrates/Exercises Multi-CPU Usage in MRI Ruby Using Ractors
#!/usr/bin/env ruby
require 'amazing_print'
require 'etc'
require 'set'
require 'shellwords'
require 'yaml'
raise "This script requires Ruby version 3 or later." unless RUBY_VERSION.split('.').first.to_i >= 3

Functional Programming in Ruby with Lambdas

Chicago Ruby Meetup, 2020-10-06, presented by Keith Bennett (@keithrbennett)

Notes and Errata:

Some practical application of these concepts can be seen in my "Writing a Command Line Application in Ruby" presentation at Paris.rb Conf in 2018 (https://www.youtube.com/watch?v=zMzzKOecvaA).

@keithrbennett
keithrbennett / split-video-to-photos
Last active October 5, 2020 18:19
Script that uses ffmpeg to create multiple JPG images from a video file.
#!/usr/bin/env ruby
if ARGV.empty?
raise 'Syntax is: split-video-to-photos video_file_spec'
end
def run_command(command)
puts command
IO.popen(command).each { |line| puts line }
end