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
import os | |
from gi.repository import GLib, Gio | |
# --- | |
def handle_lock(): | |
print("Lock") | |
def handle_unlock(): | |
print("Unlock") |
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
import os | |
from gi.repository import GLib, Gio | |
# --- | |
def handle_lock(): | |
print("Lock") | |
def handle_unlock(): | |
print("Unlock") |
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
filename="foobar" | |
File.open(filename, "r") do |fr| | |
File.open(filename + "~nop", "w") do |fw| | |
fr.each_line do |line| | |
fw.write line.gsub(".", "") | |
end | |
File.rename(filename + "~nop", filename) | |
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
require 'socket' | |
sockpath = "/tmp/foosock" | |
if ARGV.first == "server" | |
socket = UNIXServer.new sockpath | |
begin | |
while client_sock = socket.accept do | |
Thread.start do | |
puts "New connection" |
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) |
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
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
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 |
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
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! |
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 |