Skip to content

Instantly share code, notes, and snippets.

View pjb3's full-sized avatar

Paul Barry pjb3

View GitHub Profile
@pjb3
pjb3 / sequel_eager_graph.rb
Last active December 14, 2015 02:38
Why does this return a Hash instead of a Book with its chapters populated?
require 'logger'
require 'sequel'
db = Sequel.sqlite(':memory:', :logger => Logger.new(STDOUT))
db.create_table :books do
primary_key :id
String :title
end
@pjb3
pjb3 / output.txt
Created February 22, 2013 01:18
It would be cool if you could define methods on a model that return a filter and chain them, the way you can in ActiveRecord
$ ruby sequel_filter_chaining.rb
I, [2013-02-21T20:16:43.689560 #45206] INFO -- : (0.000207s) PRAGMA foreign_keys = 1
I, [2013-02-21T20:16:43.689785 #45206] INFO -- : (0.000045s) PRAGMA case_sensitive_like = 1
I, [2013-02-21T20:16:43.690238 #45206] INFO -- : (0.000347s) CREATE TABLE `people` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT, `gender` varchar(255), `born_on` date)
I, [2013-02-21T20:16:43.691695 #45206] INFO -- : (0.000569s) PRAGMA table_info('people')
I, [2013-02-21T20:16:43.697758 #45206] INFO -- : (0.000217s) SELECT sqlite_version() LIMIT 1
I, [2013-02-21T20:16:43.698478 #45206] INFO -- : (0.000071s) BEGIN
I, [2013-02-21T20:16:43.703150 #45206] INFO -- : (0.000148s) INSERT INTO `people` (`gender`, `born_on`) VALUES ('m', '1992-02-27')
I, [2013-02-21T20:16:43.706520 #45206] INFO -- : (0.002898s) SELECT * FROM `people` WHERE (`id` = 1) LIMIT 1
I, [2013-02-21T20:16:43.706702 #45206] INFO -- : (0.000058s) COMMIT
@pjb3
pjb3 / create_work.rb
Created November 3, 2012 18:10
I'm wondering why Celluloid/Sidekiq is more efficient than just doing this, having one Thread per worker
require 'json'
require 'redis'
redis = Redis.new
60.times do
redis.rpush "queue", ["Kernel", "sleep", 1].to_json
end
@pjb3
pjb3 / mutual_dependency.rb
Created October 17, 2012 15:08
By using actual class constants for classes, instead of strings, doesn't the create a mutual dependency problem?
require 'virtus'
class Post
include Virtus
attribute :title, String
attribute :comments, Array[Comment]
end
class Comment
@pjb3
pjb3 / y_u_so_slow.txt
Created October 16, 2012 00:51
Why is ruby 1.9.3 so slow to start?
$ time perl -e 'print "foo\n"'
foo
real 0m0.009s
user 0m0.004s
sys 0m0.004s
$ time ruby -e 'puts "foo"'
foo
real 0m0.184s
@pjb3
pjb3 / README.md
Created September 27, 2012 10:29
Want to run all your tests at once in a rails app?

Append the contents of 'Rakefile' to your application's Rakefile.

Now when you run:

$ rake

You will see all tests, unit, functional and integration, run at one time, without reloading the environment between each set of tests. If you want to just run one set of tests, you can still do:

$ rake test:units

$ rake test:functionals

@pjb3
pjb3 / tap_or_no_tap.rb
Created June 11, 2012 22:51
Tap that object
# To tap
def activate_person(person_id)
Person.find(person_id).tap do |person|
person.activate!
end
end
# or not to tap
def activate_person(person_id)
person = Person.find(person_id)
@pjb3
pjb3 / mail_test.rb
Created March 29, 2012 12:19
Running this with mailcatcher results in a "503 MAIL already given"
#!/usr/bin/env ruby
require 'net/smtp'
require 'time'
def send_message(smtp, from, to)
msg = %{From: #{from}
To: #{to}
Subject: test message
Date: #{Time.now.rfc2822}
@pjb3
pjb3 / batch-lazy-seq-with-fn.clj
Created February 22, 2012 16:56
I expect these to be the same, the fn one works but the recur one gives me a java.lang.IllegalArgumentException: Mismatched argument count to recur, expected: 0 args, got: 1. Any ideas?
(defn batch-lazy-seq
([] (batch-lazy-seq 1))
([start]
(let [batch-size 10
end (+ start batch-size)]
(println "Loading batch" start "-" (- end 1))
(lazy-cat
(vec (range start end))
(batch-lazy-seq end)))))
@pjb3
pjb3 / find-each-with-function.clj
Created February 22, 2012 14:48
I'm running into an issue which appears to a leaky abstraction in the way macros are defined, specifically the with-query-results macro in clojure.java.jdbc.
(require '[clojure.java.jdbc :as sql])
(def mysql-db {:subprotocol "mysql"
:subname "//127.0.0.1:3306/mydb"
:user "root"}
(defn select-rows [query]
(sql/with-query-results rs query
(into [] rs)))