Skip to content

Instantly share code, notes, and snippets.

View drhuffman12's full-sized avatar

Daniel Huffman drhuffman12

View GitHub Profile
@drhuffman12
drhuffman12 / gnuroot_wheezy86_to_jessie_jruby
Last active August 29, 2015 14:07
Upgrade gnuroot wheezy on android x86 to jessie and add Java, JRuby, Android SDK, and Ruboto gem
#
apt-get update | apt-get upgrade
# Install ant and dependancies. [Ignore errors about "libjli.so" missing; it appears to be a soft vs hard float java issue. We will replace Java in a later step.]
apt-get install ant -y
# Download Oracle Jdk 8 for Linux 32bit from: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
# ... The download should be a file like: jdk-8u20-linux-i586.tar.gz
@drhuffman12
drhuffman12 / av_splitter.rb
Last active August 29, 2015 14:08
Convert and split an audio/video file into 5 minute intervals of the specified target format and compression quality.
#!/usr/bin/ruby
# FILENAME: av_splitter.rb
#
# DESCRIPTION: Convert and split an audio/video file into 5 minute intervals of the specified target format and compression quality.
#
# USAGE:
# Put your audio/video files into the "in" sub-folder, then run the 'audio_splitter.rb' ruby script w/syntax:
# ruby -s audio_splitter.rb <format_out> '<bit_rate_info>' <debug_level>
#
@drhuffman12
drhuffman12 / sample_objs.rb
Last active August 29, 2015 14:10
Simple Object-oriented example in Ruby
# Official site for the Ruby language: "https://www.ruby-lang.org/"
class C1
attr :a, :b
def initialize(a)
@a = a
end
def self.hi
puts 'hello world'
@drhuffman12
drhuffman12 / pw_from_console.rb
Last active August 29, 2015 14:12
Get password from console via Java and JRuby
# GIST: "pw_from_console.rb" under "https://gist.github.com/drhuffman12"
# If you're dealing with a Java character array (such as password characters that you read from the console), you can convert it to a JRuby string with the following Ruby code:
jconsole = Java::java.lang.System.console()
password = jconsole.readPassword()
ruby_string = ''
password.to_a.each {|c| ruby_string << c.chr}
# .. do something with 'password' variable ..
puts "password_chars: #{password_chars.inspect}"
=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 / 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
@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 / 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 / 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 / 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