Skip to content

Instantly share code, notes, and snippets.

@mrflip
mrflip / quickie_json_to_tsv
Created March 11, 2011 01:22
Here's a brittle but mostly-worky oneliner to turn a search result into an excel-loadable file
# run this in Terminal
curl "http://api.infochimps.com/social/network/tw/search/people_search?q=qualcomm&apikey=YOURAPIKEY" | ruby -rubygems -e 'require "json" ; $_ = $stdin.read.gsub(%r{^george_api_explorer\((.*)\)$}, "\\1") ; rows = JSON.load($_); ks= rows["results"].first.keys ; ks.sort! ; rows["results"].each{|row| puts row.values_at(*ks).map{|el| el.gsub(%r{[",]+},"") }.join(",") } ' > FILE_TO_SAVE_IN.csv
@jt
jt / phone_number_regex.rb
Created June 5, 2011 19:49
Phone number regular expression pattern
def match(number)
/\+?(\d)?[-|\.|\s]?\(?(\d{3})\)?[-|\.|\s]?(\d{3})[-|\.|\s]?(\d{4})/.match number
end
# test match and captures against possibilities
[
['3335557777', nil, '333', '555', '7777'],
['333-555-7777', nil, '333', '555', '7777'],
['333.555.7777', nil, '333', '555', '7777'],
['333 555 7777', nil, '333', '555', '7777'],
@nesquena
nesquena / basic.sql
Created September 7, 2011 01:56
PostgreSQL Common Utility Queries
/* How to calculate postgreSQL database size in disk ? */
SELECT pg_size_pretty(pg_database_size('thedbname'));
/* Calculate size of a table including or excluding the index */
SELECT pg_size_pretty(pg_total_relation_size('big_table'));
SELECT pg_size_pretty(pg_relation_size('big_table')); /* without index */
/* See indexes on a table with `\d tablename` */
@fnichol
fnichol / README.md
Created February 26, 2012 01:23
A Common .ruby-version File For Ruby Projects

A Common .ruby-version File For Ruby Projects

Background

I've been using this technique in most of my Ruby projects lately where Ruby versions are required:

  • Create .rbenv-version containing the target Ruby using a definition name defined in ruby-build (example below). These strings are a proper subset of RVM Ruby string names so far...
  • Create .rvmrc (with rvm --create --rvmrc "1.9.3@myapp") and edit the environment_id= line to fetch the Ruby version from .rbenv-version (example below).

Today I learned about another Ruby manager, rbfu, where the author is using a similar technique with .rbfu-version.

@ordinaryzelig
ordinaryzelig / minitest_spec_expectations.md
Last active December 10, 2022 13:34
How to write MiniTest::Spec expectations

I'm a fan of MiniTest::Spec. It strikes a nice balance between the simplicity of TestUnit and the readable syntax of RSpec. When I first switched from RSpec to MiniTest::Spec, one thing I was worried I would miss was the ability to add matchers. (A note in terminology: "matchers" in MiniTest::Spec refer to something completely different than "matchers" in RSpec. I won't get into it, but from now on, let's use the proper term: "expectations").

Understanding MiniTest::Expectations

Let's take a look in the code (I'm specifically referring to the gem, not the standard library that's built into Ruby 1.9):

# minitest/spec.rb

module MiniTest::Expectations
@pauldix
pauldix / gist:2830675
Created May 29, 2012 21:00
ideas for fetching
# set the thread pool size and the timeout in seconds.
fetcher = Feedzirra::Fetcher.new({:thread_pool_size => 100, :timeout => 5})
# some feed data objects. also include feed_entry data objects
feeds = [Feed.new({:entries => [], etag => "..", :last_modified => "...", :url => "...", :feed_url => "...", :title => ""})]
# async style
fetcher.get(feeds, :on_success => lambda {|feed, updated_feed| ...}, :on_failure => lambda {|feed, failure_object| ...})
# that returns before finishing fetching the feeds. just adds them to a thread-safe queue to be processed by a worker pool.
# the failure condition could actually call fetcher.get on the failed feed again if you wanted to retry.
16:19 t432: What is the best way to approach the following problem.... A user logs in, application connects to database, retrieves latest 100 records and display the content. if user scrolls to bottom of page, retrieve next 100 records and display continue till end. In addition if a new record is created whilst logged in automatically display record on top... Might look familiar you use twitter
16:19 mattgordon has joined (~mattgordo@208.66.31.98)
16:20 will_: Great :)
16:20 Psi-Jack: I'm also using the primary_conninfo line in recovery.conf.
16:20 Psi-Jack: And trigger_File.
16:20 Psi-Jack: But, it's simply not streaming. :)
16:20 Psi-Jack: I'
16:20 luckyruby: t432, what web framework are u using?
16:21 orf_ has left IRC (Quit: Leaving)
16:21 bwlang_ has joined (~anonymous@70-91-134-14-ma-ne.hfc.comcastbusiness.net)
@bsingr
bsingr / actor.rb
Created August 26, 2012 16:55
Different methods to spawn Celluloid Actors.
# instantiate a cat object named 'Garfield' within its own actor thread
cat = Cat.new 'Garfield'
# blocking (the main thread waits until the cat has finished..)
cat.spray
# non-blocking
cat.spray!
# cats do what cats do
@jstorimer
jstorimer / port_scanner.rb
Created August 30, 2012 03:40
Simple, parallel port scanner in Ruby built with connect_nonblock and IO.select.
require 'socket'
# Set up the parameters.
PORT_RANGE = 1..512
HOST = 'archive.org'
TIME_TO_WAIT = 5 # seconds
# Create a socket for each port and initiate the nonblocking
# connect.
sockets = PORT_RANGE.map do |port|
@roryokane
roryokane / .rbenv-version
Created September 4, 2012 12:31
editing Wikipedia ISBN calculation code
1.9.3-p194