Skip to content

Instantly share code, notes, and snippets.

# I want to turn this:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# into this:
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
def to_grid(ary)
array = ary.dup
row_length = Math.sqrt(array.size).to_i
result = []
# In attribute file:
my_gem_packages = [
"nokigiri", # Only a gem name, without a version requirement or other options"
["bundler", "=1.0.0"], # A gem with a version requirement
["rails", "~>2.3.9", "--no-ri --no-rdoc --source http://rubygems.org"], # A gem with a version requirement and options"
["json", nil, "--source http://rubygems.org"] # Gem with options but no version requirement
]
# In recipe:
@ktheory
ktheory / simple-note-backup.rb
Created December 2, 2010 23:11
Script to automatically export simplenote data from http://simplenote-backup.appspot.com/
#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'
require 'yaml'
config = YAML.load_file(File.expand_path("~/.simple-note-backup.yml"))
email = config['email']
password = config['password']
@ktheory
ktheory / brew_completer.sh
Created January 11, 2011 15:27
Bash tab-completion for homebrew
# Bash tab-completion for homebrew (https://github.com/mxcl/homebrew)
###############
# Installation:
# mkdir ~/.bash_completion.d
# curl https://gist.github.com/raw/774554/brew_completer.sh > ~/.bash_completion.d/brew_completer.sh
# echo 'source ~/.bash_completion.d/brew_completer.sh' >> ~/.bashrc
# source ~/.bashrc
@ktheory
ktheory / README.rdoc
Created April 14, 2011 18:12
An example for logging Fog requests in a Rails app

Instrument Fog Requests in Rails

This code adds debug statements to the rails log for each fog reqests (similar to how active record shows SQL queries):

Fog AWS Compute Request (803.0ms)  [ {"Action"=>"DescribeInstances", ...} ]

The end of the request includes cumulative Fog info:

Completed 200 OK in 6043ms (Views: 2955.0ms | ActiveRecord: 319.9ms | Fog: 1642.6ms, 4 reqs)
@ktheory
ktheory / gist:966948
Created May 11, 2011 17:54
Nokogiri end_element demo
xml = %{
<doc>
<key1>value1</key1>
<key2>
Value with
line breaks
</key2>
</doc>
}
@ktheory
ktheory / proxy.rb
Created July 25, 2011 00:52
em-proxy test with Rack
#!/usr/bin/env ruby
require 'em-proxy'
Proxy.start(:host => "0.0.0.0", :port => 3000) do |conn|
conn.server :fast, :host => '127.0.0.1', :port => 3001
conn.server :slow, :host => '127.0.0.1', :port => 3002
conn.on_data do |data|
data
end
@ktheory
ktheory / gist:1147904
Created August 15, 2011 21:24
I wish this worked...
@posts.each do |post|
post.body
else
"No posts"
end
@ktheory
ktheory / .bashrc
Created September 11, 2011 21:46
Never forget `bundle exec` before running rake again...
# In your ~/.bashrc:
do_rake () {
if [ -e Gemfile ]; then
bundle exec rake $@
else
rake $@
fi
}
alias rake='do_rake'
@ktheory
ktheory / gist:1440328
Created December 6, 2011 22:27
ActiveRecord::Base#to_yaml
def to_yaml( opts = {} )
YAML::quick_emit( self, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
map.add( 'id', self.id )
end
end
ende