Skip to content

Instantly share code, notes, and snippets.

@andruby
andruby / gist:1012748
Created June 7, 2011 17:53
BERT-RPC / Ernie bug
BERTRPC::Service.new("localhost", 9999).call.ernie.echo(2282389)
@andruby
andruby / gist:1024707
Created June 14, 2011 11:18
Ruby 1.9.0 group_by benchmark
require 'rubygems'
require 'activesupport'
require 'benchmark'
include Benchmark
class T
attr_accessor :date, :count
def initialize(_date,_count)
@count = _count
@date = _date
@andruby
andruby / _introspection.rb
Created July 15, 2011 12:18
method introspection in Ruby 1.9.2
include Test::Unit::Assertions
#=> Object
m = method(:assert_in_delta)
#=> #<Method: Object(MiniTest::Assertions)#assert_in_delta>
m.methods - Object.methods
#=> [:call, :[], :arity, :to_proc, :receiver, :owner, :unbind, :source_location, :parameters]
m.parameters
#=> [[:req, :exp], [:req, :act], [:opt, :delta], [:opt, :msg]]
m.arity
#=> -3
@andruby
andruby / footnotes.rb
Created August 17, 2011 17:47
Rails-footnotes fixes for Rails 3.1 (put it in an initializer)
if defined?(Footnotes) && Rails.env.development?
Footnotes.run!
Footnotes::Filter.notes -= [:cookies, :session]
Footnotes::Notes::AssignsNote.ignored_assigns += [:@_view_renderer, :@_response_body, :@_routes, :@_view_context_class]
end
@andruby
andruby / comparison.rb
Created October 5, 2011 16:52
ruby comparison benchmark
require 'benchmark'
def is_it_true?
true
end
CONSTANT = 1
BenchTimes = 1_000_000
Benchmark.bm(20) do |bm|
@andruby
andruby / gist:1265160
Created October 5, 2011 17:57
Force disable net/http ssl certificate validation
# Dirty monkey patch to globally disable certificate validation
# YOU SHOULD NEVER USE THIS
# Except when testing/debugging with self signed certificates
require 'net/http'
module OpenSSL
module SSL
VERIFY_PEER = VERIFY_NONE
end
end
@andruby
andruby / 1-vapor.rb
Created December 1, 2011 15:47
Vapor: Validate Part Of Resource
# VAlidate Part Of Resource
module Vapor
extend ActiveSupport::Concern
included do
class_attribute :target_name, :attribute_names
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Naming
end
@andruby
andruby / fancy_debug.rb
Created December 6, 2011 15:40
Fancy Debug
class ActiveSupport::BufferedLogger
# Print a logger.debug line with colored Name
# If the msg is not a string, it will inspect it
# Pass it a block to record the time_taken to yield it
# Symbols in the msg like :method will be interpolated with block_result.send(:method)
# eg: fancy_debug("Find Users", "Found :count users") { User.where() }
def fancy_debug(name, msg = '', &block)
if block_given?
start_time = Time.now
result = yield
@andruby
andruby / inline-unpack.rb
Created December 30, 2011 17:50
ruby-inline c version of unpack for L,S and d.
inline(:C) do |builder|
builder.c "
VALUE c_unpack_l(char *bytes) {
uint32_t tmp;
memcpy(&tmp, bytes, sizeof(uint32_t));
return INT2NUM(tmp);
}"
builder.c "
VALUE c_unpack_s(char *bytes) {
uint16_t tmp;
@andruby
andruby / backtrace
Created February 14, 2012 22:03
Nanoc 3 error
> nanoc deploy --target default
Captain! We’ve been hit!
=== MESSAGE:
LoadError: cannot load such file -- fog
=== COMPILATION STACK: