Skip to content

Instantly share code, notes, and snippets.

View heftig's full-sized avatar
🦄
Horsing around

Jan Alexander Steffens heftig

🦄
Horsing around
View GitHub Profile
def events_all_accounts(time)
self.accounts.flat_map do |account|
account.events.send(time, account.now).where(:client_id => self.id)
end.uniq.sort_by { |a| a.start_time }
end
private :events_all_accounts
# all upcoming events for a client
def future_events_all_accounts
events_all_accounts(:in_future)
primes = [2]
current_number = 3
while primes.length < 10
primes.push(current_number) unless primes.any? { |p| current_number % p == 0 }
current_number += 2
end
puts primes
device_from_filename() {
# canonicalize filename
local realpath="$(readlink -e "$1")"
[[ ! -e $realpath ]] && return 1
# read /etc/mtab into associative array mtab
local -A mtab
local device mountpoint rest
while read device mountpoint rest; do
TOKEN_PROCESSORS = []
def processor(regex, &proc)
TOKEN_PROCESSORS << [regex, proc]
end
# Finds a word followed by any value
processor /\A([A-z]\w*)\s*([^\n]*)?/ do |chunk, matches|
matches[0].upcase!
attr_writer :full_name
def full_name
@full_name || "#{@first_name} #{@last_name}"
end
# encoding: utf-8
require 'rubygems'
require 'yaml'
def maybe_gem(name)
gem name
rescue Gem::LoadError
nil
end
require 'benchmark'
require 'set'
array = (1..10000).to_a
set = array.to_set
probes = 10000.times.map { rand(20000) }
Benchmark.bmbm do |bm|
bm.report("Array") { probes.each { |p| array.include? p } }
bm.report("Set") { probes.each { |p| set.include? p } }
triangle = Enumerator.new do |e|
arr = [1]
loop do
e << arr.join(" ")
arr = [1, *arr.each_cons(2).map { |x| x.inject(:+) }, 1]
end
end
while (depth = gets.to_i) > 0
lines = triangle.take(depth)
#!/usr/bin/env ruby
until (words = gets.chomp.split).empty?
stack = []
words.each do |w|
begin
stack.push Float(w)
rescue ArgumentError
args = stack.pop(Float.instance_method(w).arity + 1)
stack.push args.first.send(w, *args[1..-1])
class Enumerator
class Do < BasicObject
def initialize(enum)
@enum = enum
end
def method_missing(meth, *args, &blk)
@enum.each do |x|
x.send(meth, *args, &blk)
end