Skip to content

Instantly share code, notes, and snippets.

class ImmutabilityValidator
def validate(record)
if record.persisted?
record.errors.add(:base, "#{record.class.name} cannot be updated after created.")
end
end
end
@tyre
tyre / gist:7576558
Created November 21, 2013 05:34
Spotify Open: Free Streaming? No Thanks

You’ve doubtlessly heard something about Spotify coming to the United States. Those crazy Europeans have been rocking out to this streaming music service, according to Wikipedia, since late 2008. The tech press went positively bonkers when Spotify launched in the home of the brave. I’ve played around with the free service since day one, and have to say it is pretty neat.

It’s called Spotify Open and costs zero, although unlimited streaming likely won’t last beyond 2011. That’s a rather generous offering (read: money-pit) to sway numerous freedom-loving users to ditch iTunes and start streaming.

So what did I do?

I bought this EP off of iTunes today. For $4.99. That was free on Spotify.

@tyre
tyre / gist:9040631
Created February 16, 2014 21:06
Ruby quotes
Benchmark.bmbm do |b|
b.report('single quotes') { 37_000_000.times { 'Six sets straight, ace, cut em in the bathroom.' } }
b.report('double quotes') { 37_000_000.times { "Six sets straight, ace, cut em in the bathroom." } }
b.report('%q') { 37_000_000.times { %q{Six sets straight, ace, cut em in the bathroom.} } }
b.report('%Q') { 37_000_000.times { %Q{Six sets straight, ace, cut em in the bathroom.} } }
end
# user system total real
# single quotes 3.480000 0.000000 3.480000 ( 3.481189)
# double quotes 3.510000 0.010000 3.520000 ( 3.507431)
@tyre
tyre / pdf
Created November 16, 2014 05:43
Template PDF output
%PDF-1.7
1 0 obj
<</BitsPerComponent 8/ColorSpace /DeviceRGB/DecodeParms <</BitsPerComponent 8/Colors 3/Columns 96/Predictor 15>>/Filter /FlateDecode/Height 96/Length 19658/Subtype /Image/Type /XObject/Width 96>>
stream
xçΩî\’ï˛{c–’9KjÂBB ≤19ÿÑ¡ c`lÉ3ˇa∆Gú∆€è¡c0—&âhÇÅ@9´[›Í‹]]πn›¯~ª{¸fi[Î≠Wrw’≠{œŸgáo{ü#5;>†Î∫Á˚J†∏NÕ∂≠h4¢Èö]Û √p]+‡#EU_7u?Pt›džV≠™äÆiÆ¢h°xÉ¢jvµ§(äiöA†˙Åœ5æ‹”èF"ûÎ*öéDçP»s|À™ UU<œ’ SUU-∏É¢*ÆÌ¨H,ŒW™’äbÑÕP$ƒs=◊Æîî –MS7 œÛxñc◊tÉÍ•\é7£âèu› ézû„:é
á .Tô s‡[∂]Û=F0!œ˜¢°h$´πÆÔ{äÔVÀE3⁄æsflgøÛ”ΩØæÙň¶´C ÉaÒM«±5MgJ‹îØ3æ≈{|äÑùZM´œ¢R)s
sÛ˝¿`Ù¶Æõ!˘ÕC||1’˜]Êcò!£.J_’5@‚<•4=Âπ~(fƒ\ÀÖBëƒc1´RA¢—xå/≤L™¶E¢q◊ œ´⁄5M—XÂä®Ü“5’q,◊q+•¢¨¢mGtùA" M3y:´(ÇQïZ≠∆Äcâd≠fªÆ√«öah™Èπûi™ ¨\.∞N5ÀÊ‚H,Í{NGk√Íû÷Ωä2”—\Y–nTÀ€™2¨H,°Ú›P%0√ÜLWÛEø\è∑Ãp8(’j’∂+\ûÃ4™öÓ⁄5‰£©∫É“xBeM\«ÆîK©d∫Z©æªÈµûæ˘›Ω=,8r7LÊ¢1€6¯≈‡·Q$]=“WÕp$éÚlñç° ÅÁÜâƒEäbYU#Â[Z≠Rb`¸biEY ó´áÎ∫Ñı.óäËæÌÿ®ÀñH&U
¬lT$Bö·flı7¥x:3ØßWâ5®µ⁄`æ™f«¶¡›™∫Ê&‚qî]ÒÉp<Œ<1,”©ï—¿ÛK˘*ܧ˘õ%B[√±∏m°_
Î∆ö°,»`ˇ‡õØm~i„Îoo›>>9››fi˙Oü˙ƒÂæ"
url = "https://google.com/:bubbles/frosted"
# We want "/:bubbles/" to match, but not "/:bubbles_waffle/" to match
regex = ~r/(\/|\a)\:bubbles(\/|\Z)/
# Sad Times!
# Expect ":bubbles" to be replaced with "33"
String.replace url, regex, "\\133\\2"
# => "https://google.com/frosted"
@tyre
tyre / gist:e756f27ca8a8bab7f745
Created January 17, 2015 23:55
Readings from Interpersonal Dynamics (AKA "Touchy Feely")
"Words Can Be Windows or Walls" in Concepts and Controversy in Organizational Behavior – Marshall Rosenberg
"Asymmetries: Women and Men Talking at Cross Purposes", Chapter 1 in You Just Don't Understand: Women and Men in Conversation – Deborah Tannen
"Good Communication That Blocks Learning" by Chris Argyris in the July/August 1994 Harvard Business Review
The Relationship Cure by John M. Gottman and Joan DeClaire
"Support: Creating a Climate for Growth", in Chapter 8 of Encounter: Group Process for Intermpersonal Growth – Gerard Egan
"Have your Feelings (Or they will have you)", Chapter 5 of Difficult Conversations – Douglas Stone, Bruce Patton, Sheila Heen
def render_with_spacer([], _template, _var_key, _spacer_template) do
nil
end
def render_with_spacer([h], template, var_key, _spacer_template) do
render template, var_key => h
end
def render_with_spacer([h|rest], template, var_key, spacer_template) do
render template, var_key => h

Binary/Bitstring Matching

Introduction

Binary matching is a powerful feature in Elixir that is useful for extracting information from binaries as well as pattern matching. This article serves as a short overview of the available options when pattern matching and demonstrates a few common usecases.

Uses

Binary matching can be used by itself to extract information from binaries:

defmodule RequiredFields do
defmacro __using__(_opts) do
quote do
def new(attributes) do
Map.merge(%__MODULE__{}, attributes)
|> validate
end
def validate(struct) do
missing = missing_attributes(struct)
defmodule BitReader do
def puts_bits(bits) do
bit_string = accumulate_bits(bits)
|> Enum.join(", ")
IO.puts "<<#{bit_string}>>"
end
defp accumulate_bits(bits) do
accumulate_bits(bits, [])
end