Skip to content

Instantly share code, notes, and snippets.

View edvardm's full-sized avatar

Edvard Majakari edvardm

  • Rakettitiede Oy
  • Finland
  • 19:35 (UTC +03:00)
View GitHub Profile
@edvardm
edvardm / subtract_array.rb
Created December 17, 2012 17:31
How to remove an array from another in Ruby, functional style (no data structure is mutable)
module FunctionalArrayExtension
def head
_split_it
raise StandardError, "cannot take head of an empty array" if empty?
@head
end
def tail
_split_it
raise StandardError, "cannot take tail of an empty array" if empty?
@edvardm
edvardm / weight.rb
Created December 18, 2012 19:41
Given time in days, current weight and weight to lose, create a calendar with daily target weights
t0 = Time.local(2012, 12, 19)
time_in_days = 180
cur_w = 90.7
w_delta = 6*2.0
DAY = 86400
target_date = t0 + time_in_days*DAY
target_weight = cur_w - w_delta
daily_loss = w_delta / time_in_days
@edvardm
edvardm / holiday_calendar.rb
Last active December 10, 2015 13:58
finnish holiday calendar supporting local file cache
require 'date'
require 'tmpdir'
require 'open-uri'
require 'digest'
require 'ri_cal'
class HolidayCalendar
DEFAULT_CAL_URI = 'https://www.google.com/calendar/ical/fi.finnish%23holiday%40group.v.calendar.google.com/public/basic.ics'
CACHE_MAX_DAYS = 360
@edvardm
edvardm / hack_routing.rb
Created January 11, 2013 13:48
rails url_for hack with prefix
# initializers/fix_relative_root_cells.rb
fail unless ActionDispatch::Routing::RouteSet
module ActionDispatch
module Routing
class RouteSet
alias url_for__ptsroot__ url_for
def url_for(options = {})
options[:script_name] = ENV['RAILS_RELATIVE_URL_ROOT'] if options.kind_of?(Hash)
options = Base.relative_url_root.to_s + options if options.kind_of?(String) and options.starts_with?('/')
url_for__ptsroot__(options)
@edvardm
edvardm / gist:4714421
Created February 5, 2013 13:23
general extract function for kourne shells
x () {
if [ -f $1 ]
then
case $1 in
(*.tar.bz2) tar xvjf $1 ;;
(*.tar.gz) tar xvzf $1 ;;
(*.bz2) bunzip2 $1 ;;
(*.rar) unrar x $1 ;;
(*.gz) gunzip $1 ;;
(*.tar) tar xvf $1 ;;
[user]
email = edvard@majakari.net
name = Edvard Majakari
[alias]
bl = blame
br = branch
ci = commit
cl = clone
co = checkout
cp = cherry-pick
@edvardm
edvardm / gist:5097670
Created March 6, 2013 08:36
Simple redis cache helper, always evaluates value if Redis is down
module RedisHelper
def redis
@redis ||= Redis.new.tap do
@redis_available = true
end
end
def ns_key(k)
"enfo_sm:#{k}"
end
@edvardm
edvardm / thread_pool.rb
Created March 7, 2013 10:14
Simple thread pool by Kim Burgestrand, with ThreadPool.run convenience method
require 'thread'
class ThreadPool
def self.run(numthreads, &block)
tp = new(numthreads)
block.call(tp)
ensure
tp.shutdown
end
@edvardm
edvardm / gist:5300071
Created April 3, 2013 10:27
simple curses-based gof
require 'curses'
class GameOfLife
LIVE_CHAR = "0"
DEAD_CHAR = '.'
def initialize(nrows, ncols, opts={}, &block)
@nrows, @ncols = nrows, ncols
@opts = opts
@rows = (0..@nrows).each_with_object([]) { |i, acc| @ncols.times { (acc[i] ||= []) << DEAD_CHAR } }
class Hash
class Mediator < BasicObject
def initialize(hash, *syms)
@h = hash
@syms = syms
end
def method_missing(name, *args)
@h.has_key?(name) ? @h[name] : super
end