Skip to content

Instantly share code, notes, and snippets.

# MAC manipulators
alias random_mac='sudo ifconfig en0 ether `openssl rand -hex 6 | sed "s/\(..\)/\1:/g; s/.$//"`'
alias restore_mac='sudo ifconfig en0 ether YOUR_ORIGINAL_MAC_ADDRESS_GOES_HERE'
anonymous
anonymous / concerning.rb
Created December 18, 2012 18:00
class Module
# We often find ourselves with a medium-sized chunk of behavior that we'd
# like to extract, but only mix in to a single class.
#
# We typically choose to leave the implementation directly in the class,
# perhaps with a comment, because the mental and visual overhead of defining
# a module, making it a Concern, and including it is just too great.
#
#
# Using comments as lightweight modularity:
@fxn
fxn / atomic_redis_extensions_with_lua.md
Created December 11, 2012 18:55
Atomic Redis Extensions with Lua

Atomic Redis Extensions with Lua

Redis has a very simple execution model: while the server is evented and is able to cope with many concurrent clients, the commands themselves are executed one after the other. Redis is single-threaded. So, you know that while a LPUSH runs, say, no other command can possibly read or change anything at the same time.

If you open a transaction with MULTI, the commands of the transaction are queued, and when the transaction is committed, the queued commands run atomically. No other clients are served while they run because EXEC, as the rest of ordinary commands, is executed in that single thread. Other clients may be served while the commands are queued though, and that gives place to optimistic idioms with WATCH.

Redis 2.6 comes with support for Lua scripting, and that yields new options. because **Lua script

@ngauthier
ngauthier / README.md
Created November 19, 2012 15:43
Model Accessors

Model Accessors

I really like this pattern for providing accessors to the models used in a controller. Alternatively, you usually see a before_filter to set the model from the params hash only on the actions that need it. IMO this is abusing a before_filter because the filter chain is for pre-processing the request. This does sort of make sense if you want to redirect away from the page gracefully if the record can't be found, but you can do that by just calling the accessor in a filter to make sure it's there.

This means that views are more like partials with locals, which is nice. It also means the accessor is as easy to test as a named filter. And as usual it pulls the method out of the action to share, which is also nice.

Also, it achieves the same ends as gems that try to OO the view, but without a gem and with terse code. The model can be swapped for a presenter if necessary.

Capybara.add_selector :record do
xpath { |record| XPath.css("#" + ActionController::RecordIdentifier.dom_id(record)) }
match { |record| record.is_a?(ActiveRecord::Base) }
end
# This allows developers to pass an Active Model object
# to within, check and uncheck. This is very useful when
# used with `content_tag_for` since `content_tag_for`
# generates an unique dom id for each object. With this
# module, we can write:
#
# within comment do
# click_link "Destroy"
# end
#
@jeremy
jeremy / gist:4035286
Created November 7, 2012 23:15
Enumerable#associate

We've seen lots of Ruby feature requests about converting an Enumerable to a Hash: to_h, map_hash, map_to, each_with_hash, etc. Let's look at one common, simple case of this.

Building a key/value mapping from a collection of keys is awkward. We commonly see Hash[*collection.map { |element| [element, calculate(element)] }] or collection.each_with_object({}) { |element, hash| hash[element] = calculate(element) }. Both are verbose. They require boilerplate code that's not relevant to the programmer's intent: to associate an enumerable of keys with calculated values.

Ruby has the idea of an association already: a key and value paired together. It's used by Array#assoc to look up a value from a list of pairs and by Hash#assoc to return a key/value pair. Building up a mapping of key/value pairs is associating keys with values.

So! Consider Enumerable#associate which builds a mapping by associating keys with values:

# Associate filenames with URLs. Before:
@jarek-foksa
jarek-foksa / safari6-webkit-inspector.md
Created October 16, 2012 08:32
How-to: Restore WebKit Web Inspector on Mountain Lion

Restore WebKit Web Inspector on Mountain Lion

  1. Download WebKit build r121872: http://builds.nightly.webkit.org/files/trunk/mac/WebKit-SVN-r121872.dmg
  2. Right-click on WebKit.app and choose Show Package Contents
  3. Navigate to ./Contents/Frameworks/10.x/WebCore.framework/Versions/Current/Resources/ and you will find a folder called inspector. This contains the HTML/CSS/JavaScript for the Web Inspector, awesome! Keep this Finder window open!
  4. Open a new Finder window and navigate to /System/Library/PrivateFrameworks/WebInspector.framework/Versions/Current/Resources. This is where the 'new' Web Inspector is located.
  5. Now copy the contents from the WebKit Web Inspector folder (from Step 3.) to the new Web Inspector folder from Step 4. Do not remove anything, but when it prompts to 'keep newer' files press the Replace button.
  6. Now there is only one thing left to do, remove of rename the Main.html file (this is the main frame for the new Web Inspector) and rename inspector.html to `Main.ht
@mislav
mislav / easy_way.rb
Last active May 20, 2020 13:48
RESOLVE SHORT URLS before storing. Short URLs are for microblogging; you should never actually keep them around.
require 'net/http'
# WARNING do not use this; it works but is very limited
def resolve url
res = Net::HTTP.get_response URI(url)
if res.code == '301' then res['location']
else url.to_s
end
end
@ream88
ream88 / Gemfile
Created August 27, 2012 10:39
Sprockets/Asset Pipeline in every project
source :rubygems
gem 'actionpack', '~> 3.2', require: 'sprockets/static_compiler'
gem 'filewatcher'
gem 'rake'
gem 'sprockets'
gem 'coffee-script'
gem 'sass'
gem 'uglifier'