Skip to content

Instantly share code, notes, and snippets.

Avatar

Keith Bennett keithrbennett

View GitHub Profile
@keithrbennett
keithrbennett / module-class-state.rb
Last active Apr 13, 2021
Illustrates that Ruby modules *can* have state but only on the module level, not the instance level.
View module-class-state.rb
#!/usr/bin/env ruby
module M
class << self
attr_accessor :foo
end
end
class C1
def call
View gist:18f10124354d62eb8ba5feafaa9b39dc
#!/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 Feb 8, 2021
Removes Unicode "\u200B" zero width spaces; useful for copying source code from O'Reilly learning books
View strip-zero-width-spaces.rb
#!/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 Feb 7, 2021
Binary search method in Ruby - < works better than <=, right?
View binary_search.rb
#!/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 Feb 1, 2021
Illustrates differences in behavior between (until, for) and (loop, map, upto).
View test_iteration_constructs.rb
#!/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 Jan 30, 2021
Ruby script that outputs to stdout the project URL corresponding to a git remote URL
View git-project-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 Jan 18, 2021
Illustrates/Exercises Multi-CPU Usage in MRI Ruby Using Ractors
View my-ractor.rb
#!/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
View ruby-lambda-pres-notes-and-errata.md

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 Oct 5, 2020
Script that uses ffmpeg to create multiple JPG images from a video file.
View split-video-to-photos
#!/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
@keithrbennett
keithrbennett / groutes
Created Sep 6, 2020
Modification of @abachman's helpful groutes utility to add branch support
View groutes
#!/bin/bash
# groutes greps routes
#
# the first time you run routes it runs bin/rails routes and caches it and
# greps that. if you update config/routes.rb, groutes _knows_ and rebuilds
# the cache.
#
# usage:
# $ groutes username