Skip to content

Instantly share code, notes, and snippets.

View hannestyden's full-sized avatar

Hannes Tydén hannestyden

  • Stockholm, Sweden
View GitHub Profile
# http://rubular.com/r/UMTb5KhY8A
DATE=/\d{4}-\d{2}-\d{2}/
ZONE=/Z|[-+]\d{2}(?::\d{2})?/
TIME=/\d{2}(?::\d{2}(?::\d{2}(?:\.\d+)?)?)?/
TIME_WITH_ZONE=/#{TIME}(?:#{ZONE})?/
ISO8601_FORMAT=/^(#{DATE})(?:T(#{TIME_WITH_ZONE}))?$/
ISO8601_FORMAT.match('2008-08-30T01:45:36.100Z') # => #<MatchData "2008-08-30T01:45:36.100Z" 1:"2008-08-30" 2:"01:45:36.100Z">
def scream(s)
s.upcase
end
s = scream(<<-TEXT.gsub('z', 's')).gsub('EE', 'I')
Theez eez a lot of text.
Not really, but eet could be.
TEXT
@hannestyden
hannestyden / hide_removed_lines_in_gh_diff.js
Created November 26, 2013 09:47
Hide removed lines in GitHub diffs.
$('tr.file-diff-line.gd').hide();
git branch --merged | grep -v "\*" | while read b; do git br -d $b; done
git branch | grep -v "\*" | while read b; do echo "Delete '$b'?"; read y < /dev/tty; [[ "$y" == "y" ]] && git br -D $b; done
class Human
def greet
"I am a #{species}"
end
def species
'human'
end
end
Human.new.greet # => "I am a human"
module Utils; def r; yield rescue $!; end; end; include Utils
def i
:i
end
def I
:I
end
class A # < Struct
def self.new(mod, *arguments, &block)
if arguments.length > 0
# Why is the block required in order to access the members from a block?
# ... because the block is passed to the `super`
Struct.new(*arguments) # { }
else
Class.new
end.tap { |c|
c.send(:include, mod)
class A < Struct
def self.new(mod, *arguments, &block)
if arguments.length > 0
super(*arguments) do
include mod
define_method :greet, &block
end
else
Class.new do
include mod
def tapp(o, &block)
o.tap { |m| p block.call(o) }
end
tapp("haha", &:size)
# => "haha"
# >> 4
@hannestyden
hannestyden / gist:6838642
Last active December 24, 2015 17:49
Protect my own file from myself. The intention is to modify the file through functions which use `sudo`.
touch /tmp/sandwich
sudo chown root:wheel /tmp/sandwich
echo "make me a sandwich" >> /tmp/sandwich
sudo sh -c 'echo "make me a sandwich" >> /tmp/sandwich'
cat /tmp/sandwich