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
# Always wanted to chain comparisons the snaky way?
#
# puts 'Right there!' if 5 < x <= 15
#
# There you go.
class FalseClass
['<', '>', '<=', '>='].each do |op|
eval "def #{op}(other) false end"
end
#!/usr/bin/env ruby
require "librevox"
require "sequel"
DB = Sequel.connect('postgres://postgres:leenuhks@localhost/freeswitch')
class Outbound < Librevox::Listener::Outbound
def session_initiated
answer
# Returns a hash of methods organized by their owner and method type.
module Kernel
def methods
output = {}
syms = [:public_methods, :protected_methods, :private_methods]
syms.each do |sym|
output[sym] = {}
send(sym).each do |m|
key = self.method(m).owner
@heftig
heftig / gist:1164406
Created August 23, 2011 05:24 — forked from dmonopoly/gist:1164395
nil and false notion...
def mealtimes(meal)
arr = []
arr << "breakfast" if meal.breakfast
arr << "lunch" if meal.lunch
arr << "dinner" if meal.dinner
arr.join(", ").sub(/(.*),/, "\\1 and")
end
# Need to block
begin
json = Timeout.timeout 3 do
Net::HTTP.new(uri.host, uri.port).start do |https|
https.request_get(uri.request_uri)
end
end
rescue Timeout::Error
if @retries >= 3
return {return: 'Gateway Timeout.', response: 504}
def fetch_ftp
ftp = Net::FTP.new(self.meta[:host])
ftp.login(user = self.username, passwd = self.password)
buffer = ""
ftp.getbinaryfile(self.url) { |data| buffer << data }
ftp.quit()
MultiXml.parse(buffer) rescue nil
attr_writer :full_name
def full_name
@full_name || "#{@first_name} #{@last_name}"
end
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
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)
class PokerGame
include Enumerable
include Comparable
attr_reader :hands, :deck
RANKS = %w{ 2 3 4 5 6 7 8 9 T J Q K A }
SUITS = %w{ S H D C }
def initialize(hands, cards)
@deck = RANKS.product(SUITS).map(&:join)