Skip to content

Instantly share code, notes, and snippets.

View patshaughnessy's full-sized avatar

Pat Shaughnessy patshaughnessy

View GitHub Profile
@patshaughnessy
patshaughnessy / application.css.less
Created November 23, 2011 20:38
application.css.less example overriding Twitter Bootstrap variable (using less-rails-bootstrap)
*
* application.css.less
*= require_self
*/
@import "twitter/bootstrap/reset.less";
@import "twitter/bootstrap/variables.less";
// Override link color for my app:
@patshaughnessy
patshaughnessy / application.css.scss
Created November 23, 2011 20:39
application.css.scss example overriding Twitter Bootstrap variables (using bootstrap-sass)
/*
* application.css.scss
*= require_self
*/
@import "bootstrap/reset.css.scss";
@import "bootstrap/variables.css.scss";
// Override link color for my app:
def avatar_geometry(style = :original)
@geometry ||= {}
begin
path = avatar.path(style)
unless path
temp_file = Tempfile.open("#{avatar_file_name}_#{style}")
temp_file << avatar.file_contents(style)
path = temp_file.path
end
@geometry[style] ||= Paperclip::Geometry.from_file path
@patshaughnessy
patshaughnessy / hostess.rb
Created January 29, 2012 19:17
Part of the Hostess class from RubyGems.org
class Hostess < Sinatra::Base
... etc ...
def serve_via_s3
serve do
redirect "http://#{$rubygems_config[:s3_domain]}#{request.path_info}"
end
end
@patshaughnessy
patshaughnessy / pusher.rb
Created January 29, 2012 19:19
Part of the Pusher class from RubyGems.org
class Pusher
attr_reader :user, :spec, :message, :code, :rubygem, :body, :version, :version_id
def initialize(user, body, host_with_port=nil)
@user = user
@body = StringIO.new(body.read)
@indexer = Indexer.new
@host_with_port = host_with_port
end
def cripple_rubygems(specs)
reverse_rubygems_kernel_mixin
executables = specs.map { |s| s.executables }.flatten
Gem.source_index # ensure RubyGems is fully loaded
::Kernel.send(:define_method, :gem) do |dep, *reqs|
...etc...
@patshaughnessy
patshaughnessy / gist:1979298
Created March 5, 2012 17:01
Ruby parse string backtrace
(gdb) bt
#0 str_new (klass=4303856320, ptr=0x100680500 "The Ruby core team is playing a joke on us!", len=43) at string.c:389
#1 0x00000001001179f8 in rb_str_new [inlined] () at /Users/pat/.rvm/src/ruby-1.9.3-preview1/string.c:416
#2 0x00000001001179f8 in rb_enc_str_new (ptr=<value temporarily unavailable, due to optimizations>, len=<value temporarily unavailable, due to optimizations>, enc=0x1004027e0) at string.c:430
#3 0x000000010009f725 in parser_str_new (p=<value temporarily unavailable, due to optimizations>, n=<value temporarily unavailable, due to optimizations>, enc=0x1004027e0, func=2, enc0=0x1004027e0) at parse.y:5401
#4 0x00000001000aa677 in parser_parse_string (parser=0x10060e000, quote=0x1010793e0) at parse.y:6033
#5 0x00000001000ab285 in parser_yylex (parser=0x10060e000) at parse.y:6645
#6 0x00000001000b4999 in ruby_yyparse (parser=<value temporarily unavailable, due to optimizations>) at parse.y:7922
#7 0x00000001000c24bf in yycompile0 (arg=<value temporarily unavailable, due to optimi
I go to the home page
And I follow "sign up"
And I fill in username with "Matt"
And I fill in password with…
@patshaughnessy
patshaughnessy / gist:3105631
Created July 13, 2012 15:50
Why is for faster than each on Ruby 1.8.7?

Great question! It turns out that in Ruby 1.9 or 2.0 it's actually faster to use each, and not for (by a similar very small amount).

Looking into why for is faster than each in Ruby 1.8.7, it turns out Ruby 1.8.7 uses a slightly different code path for each vs. for, although for still is automatically evaluated as a call to each internally:

if (nd_type(node) == NODE_ITER) {
  result = rb_eval(self, node->nd_iter);
}
else {
  VALUE recv;
The "tostring" instruction below calls into this C code, from string.c. This checks if the expression/object is already a string, and does nothing if it is. If it's not a string, it calls "to_s" on the object.
VALUE
rb_obj_as_string(VALUE obj)
{
VALUE str;
if (RB_TYPE_P(obj, T_STRING)) {
return obj;
}