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 | |
# 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] |
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
@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 |
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
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 |
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 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 |
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
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 |
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
# 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) |
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
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) |
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 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 |
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
=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. |
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/ruby | |
=begin | |
## USAGE: | |
# Ruby: | |
ruby gem-fetch-dependencies.rb fetch <gem_name> --dependencies | |
# JRuby: | |
jruby gem-fetch-dependencies.rb fetch <gem_name> --dependencies |