Skip to content

Instantly share code, notes, and snippets.

require "money"
class Decorator < BasicObject
undef_method :==
def initialize(component)
@component = component
end
def method_missing(name, *args, &block)
@lkfken
lkfken / gist:2967945
Created June 21, 2012 19:25
Verify the unpack string
require 'terminal-table'
def unpack_str_table(unpack_str, data = nil)
len = unpack_str.scan(/\d+/)
sum = 0; idx = 1
fields = len.map { |x| sum += x.to_i }
headings = %w( Index Length Start End )
headings << "Data" unless data.nil?
table = Terminal::Table.new :headings => headings
fields.each do |stop|
@lkfken
lkfken / gist:2974896
Created June 22, 2012 20:10
Simplest Possible Ruby Web Server: Using Rack for a one-liner
#credit to: http://phrogz.net/simplest-possible-ruby-web-server
ruby -rrack -e "include Rack;Handler::Thin.run Builder.new{run Directory.new''}"
@lkfken
lkfken / gist:3014516
Created June 28, 2012 22:54
Random URL-safe tokens with SecureRandom
require 'securerandom'
SecureRandom.urlsafe_base64(8)
@lkfken
lkfken / Gemfile
Created November 3, 2015 22:30
Using JDBC & Sequel to connect to SQLite3
source 'https://rubygems.org'
gem 'jdbc-sqlite3', :platform => :jruby
gem 'sequel'
@lkfken
lkfken / app.rb
Created November 3, 2015 22:41
Sequel #with_recursive CTE
DB = Sequel.connect('jdbc:sqlite::memory:')
DB.create_table(:categories) do
Integer :id, :primary_key => true
foreign_key :parent_id, :categories
String :name, :null => false
end
DB[:categories].import([:id, :parent_id, :name], [[1, nil, 'GPS'], [2, 1, 'Vehicle GPS'], [3, 1, 'Handheld GPS'], [4, nil, 'GPS Accessories']])
DB.create_table(:products) do
@lkfken
lkfken / bcrypt_example.rb
Created June 11, 2016 00:41
Ruby BCrypt example
require 'bcrypt'
require 'pp'
secret = 'my password'
BCrypt::Engine.cost = 4 # default is 10
password = BCrypt::Password.create(secret)
puts '********** create **********'
puts ['version:', password.version].join(' ')
@lkfken
lkfken / sequel_sqlite_example.rb
Last active July 25, 2016 01:18
Convert CSV to Sqlite records.
require 'sequel'
require 'csv'
csv_records = CSV.read('records.csv')
DB = Sequel.connect('jdbc:sqlite::memory:')
DB.create_table(:records) do
Integer :id, :primary_key => true
String :member_id, :null => false
@lkfken
lkfken / vlc_d100.rb
Last active December 1, 2017 17:34
save audio stream to .m4a file using VLC
require 'uri'
require 'pp'
require 'pathname'
url = URI('http://66.55.135.155:8000/Channel1')
##### VLC #####
stop_time = ARGV.shift # duration in secords
abort 'no stop tiime defined' unless stop_time
@lkfken
lkfken / extract_year.rb
Created March 28, 2017 23:36
Sequel: add modulus support
ds_1 = People.select(:birth).select_append{year(:birth).as(:year)}
ds_1.first ##<Member @values={:birth=>1980-09-15 00:00:00 -0700, :year=>1980}>
ds_2 = People.select(:birth).select_append{(year(:birth) % 100 ).as(:year)}
# ds_2.first
# NoMethodError: undefined method `%' for #<Sequel::SQL::Function @name=>:year, @opts=>{}, @args=>[:birth]>
# Mostly because it is less common. It's defined in BitwiseMethods, which is only included in NumericExpression by default.
# Some of the BitwiseMethods overlap with the BooleanMethods that are included in GenericExpression.