This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attr_writer :full_name | |
def full_name | |
@full_name || "#{@first_name} #{@last_name}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer