Skip to content

Instantly share code, notes, and snippets.

View RoUS's full-sized avatar

Ken Coar RoUS

View GitHub Profile
@RoUS
RoUS / max-product.rb
Created December 14, 2011 18:29
Euler Challenge: maximum product from 5 consecutive digits in a 1000-digit integer
#! /usr/bin/env ruby
#
# PROBLEM:
# Find the greatest product of five consecutive digits in the 1000-digit number.
#
# 73167176531330624919225119674426574742355349194934
# 96983520312774506326239578318016984801869478851843
# 85861560789112949495459501737958331952853208805511
# 12540698747158523863050715693290963295227443043557
# 66896648950445244523161731856403098711121722383113
@RoUS
RoUS / toujours-debugger.rb
Created May 30, 2012 19:39
Keep your 'debugger' statements in even when testing on hosts lacking the gem
#
# This snippet allows you to sprinkle your code with #debugger references
# even if the ruby-debug gem isn't installed (or there's some other error
# encountered loading it).
#
begin
require('ruby-debug')
Debugger.start
rescue Exception => e
unless (Kernel.method_defined?(:debugger))
@RoUS
RoUS / Array.enrange.rb
Created September 6, 2012 14:16
[Ruby 1.8.7] Reduce consecutive elements in an array to ranges
class Array
#
# Return an array with all occurrences of 2 or more logically
# successive elements replaced with ranges, and disjoint elements
# as-is.
#
# @param [Boolean] sorted
# If this parameter can be evaluated as a Boolean 'true' value, the
# current array will be copied and the copy sorted before
@RoUS
RoUS / configreader.sh
Created August 16, 2014 01:03
Bash function to read keys/values from an ini-style file section into an associative envariable
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# vim: ts=4:sw=4:noet
#
function _read_config_file() {
local _cfgfile='/dev/stdin'
local _section
local _varname='CONFIG_HASH'
local _line
local _key
local _value
@RoUS
RoUS / wrap.rb
Created January 15, 2015 18:14
OptionParser helper to wrap long option descriptions
#
# Long OptionParser descriptions don't get nicely wrapped by it, so
# this method does the appropriate fill, wrap, and indent.
#
# @license Apache 2.0
#
# @overload wrap(*args)
# @param [Array<Hash,String>] args
# The argument list can include multiple strings and hashes.
#
@RoUS
RoUS / keem.sh
Created April 15, 2015 21:19
Simple-minded Bash script for common GPG management tasks
#! /bin/bash
# -*- coding: utf-8 -*-
#--
# Copyright © 2015 Ken Coar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@RoUS
RoUS / dirchange.bash
Created January 7, 2016 17:25
Bash functions for doing things on cd into/out of directories
#
# This function provides for sourcing of shell scripts when moving
# into (or out of) a directory.
#
# If the command exits with a non-zero status, or the working
# directory isn't changed, no scripts are processed.
#
# If the command exits with a 0 status and the current working
# directory does change, the previous directory is checked for an
# ".on_exit" file. If found, it gets sourced. Similarly, the new WD
@RoUS
RoUS / block-arg-array-deconstruction.rb
Created February 3, 2019 22:06
Deconstructing arrays in block arguments
h1 = {
:one => 1,
:two => 2,
:three => 3,
}
# h1 => { :one => 1, :two => 2, :three => 3 }
#
# Each key/value pair is passed as a two-element array as the second
# block argument -- so you can deconstruct it in the arglist declaration.
@RoUS
RoUS / gist:3c4ca47a971ed0bd4d2b86d501eb1dbf
Created June 11, 2019 21:09
Fedora 29, ruby-2.5.5 - gem command fails
Fedora 29
# rpm -q ruby rubygems
ruby-2.5.5-101.fc29.x86_64
rubygems-2.7.6.2-101.fc29.noarch
# which gem
/usr/bin/gem
[root@Helium etc]# gem
/usr/share/ruby/yaml.rb:5: warning: It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
Traceback (most recent call last):