Skip to content

Instantly share code, notes, and snippets.

View drhuffman12's full-sized avatar

Daniel Huffman drhuffman12

View GitHub Profile
@drhuffman12
drhuffman12 / xml_formatter.rb
Last active March 23, 2017 19:05
xml_formatter
#!/usr/bin/env ruby
# USAGE:
# $> ./xml_formatter path/from/my/file.xml
# $> ./xml_formatter path/from/my/file.xml path/to/my/file_formatted.xml
require 'nokogiri'
in_file = ARGV[0]
out_file = ARGV[1]
@drhuffman12
drhuffman12 / windows_to_linux_line_endings.rb
Last active January 27, 2017 23:49
Replace all "\r\n" occurances with "\n" for all files in a folder
@folder_path = 'path/to/your/folder'
Dir.glob(File.join(@folder_path,'*')).each {|f| File.open(f, 'w', crlf_newline: false) {|f| f.write(File.read(f).gsub("\r\n","\n") } };nil
@drhuffman12
drhuffman12 / hash_utils.rb
Created November 30, 2016 19:03
utility methods for hashes
class HashUtils
# See also various flattening solutions at http://stackoverflow.com/questions/23521230/flattening-nested-hash-to-a-single-hash-with-ruby-rails
def self.flatten_hash(param, prefix='')
param.each_pair.reduce({}) do |a, (k, v)|
v.is_a?(Hash) ? a.merge(flatten_hash(v, "#{prefix}#{k}.")) : a.merge("#{prefix}#{k}".to_sym => v)
end
end
end
class Hash
@drhuffman12
drhuffman12 / star_curves.rb
Created November 2, 2016 03:18
simple txt grapher
def stars(y,scale,char);y>0 ? char * ((y*scale).round) : '';end
def curve(n);(0..n+1).each.collect{|x| y=Math.sin(Math::PI*2*x/n); [x, y]};end
def star_set(y,scale,char_neg,char_zero,char_pos); yn = y < 0 ? stars(-y,scale,char_neg) : ''; yp = y > 0 ? stars(y,scale,char_pos) : ''; ("%#{scale.round.to_i}s" % yn) + char_zero + ("%-#{scale.round.to_i}s" % yp); end
def star_set_def(y);star_set(y,10,'-','.','+');end
puts curve(16).collect{|n| star_set_def(n[1])+" #{n[0]}, #{n[1]}\n"}.join('')
=begin
. 0, 0.0
.++++ 1, 0.3826834323650898
@drhuffman12
drhuffman12 / env_printer.rb
Created November 1, 2016 18:33
Snippet for printing ENV key-value pairs, w/ keys right-aligned and values left-aligned
puts
longest_key = ENV.keys.max_by(&:length)
key_formatter = '%' + "#{longest_key.length}" + 's'
puts "longest_key: '#{longest_key}'"
puts "key_formatter: '#{key_formatter}'"
puts "ENV:"
puts
ENV.each_pair do |k,v|
puts "#{key_formatter % k}: #{v}"
end
@drhuffman12
drhuffman12 / po2.rb
Created October 17, 2016 20:36
Check if numbers are power of 2
# based on http://stackoverflow.com/questions/29718767/check-if-array-elements-are-power-of-two
require 'benchmark'
class Po2
def self.via_to_s(n)
(n.to_s(2) =~ /^10*$/) ? 0 : 1 # faster for more than about 100 numbers
end
def self.via_log(n)
@drhuffman12
drhuffman12 / test_fiber_d_loops.cr
Last active September 28, 2016 11:22
Benchmark threads (Conclusion: best is either loops or fibers, depending on use case; threads are problematic, tending to either lock or crash the app)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
@drhuffman12
drhuffman12 / rvm_proj_new.sh
Last active September 9, 2016 17:56
Helper script to create a Ruby app folder (w/ a '.versions.conf' file) and configure RVM
#!/usr/bin/env bash
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
# echo $me
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]
then
echo
echo 'To create a Ruby app folder and configure it for RVM usage, use the following syntax:'
echo
echo ' bash --login './$me 'PROJ_ROOT PROJ_NAME PROJ_RUBY_VER PROJ_GEMSET_NAME'
echo
=begin
https://gist.github.com/drhuffman12/a5ad839b71e8ca334a2a
Problem statement:
Create a class named PalindromeDetector. Within that class, implement a
static function named GetPalindromeYears that takes a start and end date as parameters and
returns a distinct set of Dates in chronological order(inclusive of start and end dates)
that, when formatted as MMddyyyy, are palindromes.
@drhuffman12
drhuffman12 / gem-fetch-dependencies.rb
Last active April 12, 2018 12:40 — forked from Milly/gem-fetch-dependencies
Extend 'gem fetch' command. Fetch all dependencies. Handles OS (Windows vs non-Windows) and Ruby Platform (JRuby vs Ruby).
#!/usr/bin/ruby
=begin
## USAGE:
# Ruby:
ruby gem-fetch-dependencies.rb fetch <gem_name> --dependencies
# JRuby:
jruby gem-fetch-dependencies.rb fetch <gem_name> --dependencies