Skip to content

Instantly share code, notes, and snippets.

View pjb3's full-sized avatar

Paul Barry pjb3

View GitHub Profile
@pjb3
pjb3 / bootstrap.html
Created February 19, 2014 04:40
Basic bootstrap template using CDN for all assets
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
@pjb3
pjb3 / default_values.rb
Created January 14, 2014 16:07
Looking when default arguments are resolved in Ruby 2.0
def foo(x: wtf)
end
puts "wtf did not get called when the method was defined"
foo x: 1
puts "wtf did not get called when we passed a value for x"
begin
@pjb3
pjb3 / confusing.rb
Created January 7, 2014 04:53
This is why people get class_eval and instance_eval confused in Ruby. You create class methods with instance eval and instance methods with class eval.
class Foo
instance_eval <<-end_eval, __FILE__, __LINE__
def bar
"class method"
end
end_eval
class_eval <<-end_eval, __FILE__, __LINE__
def bar
"instance method"
@pjb3
pjb3 / config.ru
Created December 27, 2013 15:33
Why don't Firefox, Chrome or Safari support passing HTTP Basic Auth user/pass in the URL the way cURL does? I am running the rack server with the rackup command. In all of these requests, I am loading the URL http://user:pass@localhost:9292. In the output, you can tell which client is used for each request from the user agent header. I tried cur…
require 'json'
run -> (env) {
body = JSON.pretty_generate(env) + "\n"
puts body
[ 200, { 'Content-Type' => 'application/json',
'Content-Length' => body.length.to_s }, [body] ]
}
@pjb3
pjb3 / bench.rb
Created December 12, 2013 00:45
A benchmark of calling methods in different ways
require "benchmark"
test = "hi man"
m = test.method(:length)
n = 100000
Benchmark.bmbm {|x|
x.report("meth") { n.times { test.length } }
x.report("call") { n.times { m.call } }
x.report("send") { n.times { test.send(:length) } }
x.report("eval") { n.times { eval "test.length" } }
}
@pjb3
pjb3 / irb_output.txt
Created October 1, 2013 00:21
Rails 4 appears to log SQL queries in the console in development by default. If it doesn't, I can't figure out why it is for me.
$ cat ~/.irbrc
cat: /Users/pbarry/.irbrc: No such file or directory
$ rails c
Loading development environment (Rails 4.0.0)
irb(main):001:0> Subscription.first
Subscription Load (2.5ms) SELECT "subscriptions".* FROM "subscriptions" ORDER BY "subscriptions"."id" ASC LIMIT 1
=> nil
irb(main):002:0> puts ENV.keys.sort
Apple_PubSub_Socket_Render
Apple_Ubiquity_Message
@pjb3
pjb3 / wat.html
Last active December 20, 2015 17:19
Why is the inner div underlined?
<html>
<body>
<div style="text-decoration: underline; font-weight: bold">
I am underlined
<div style="text-decoration: none; font-weight: normal">
I am not
</div>
</div>
</body>
</html>
@pjb3
pjb3 / gist:5279741
Last active December 15, 2015 15:09
Default values in Hashes
# The inner Hash.new(0) will now be the object that returned
# when you try to fetch the value for a key that doesn't exist
>> h = Hash.new(Hash.new(0))
=> {}
# When you do this, you are mutating the object that is the default value
# of the Hash and not changing the Hash itself
>> h[:foo][10] += 1
=> 1
@pjb3
pjb3 / query.rb
Created March 20, 2013 14:09
It's things like this that make Ruby fun :)
class Query
def initialize
@hash = {}
end
def method_missing(name, *args)
value = args.length > 0 ? args.first : true
@hash.merge!(name => value)
self
end
# Why do I have to have two levels of blocks?
FactoryGirl.define do
factory :user do
name "test"
end
end
# Why can't I just do something like this?
FactoryGirl.factory(:user) do
name "test"