Skip to content

Instantly share code, notes, and snippets.

View betawaffle's full-sized avatar

Andrew Hodges betawaffle

View GitHub Profile
def split_isbn
return if isbn.nil?
x = 4
case isbn[0..2].to_i
when 0..19, 100..109 then x = 2
when 20..69, 110..139 then x = 3
when 70..84, 140..154 then x = 4
when 85..89, 155..185 then x = 5
when 90..95, 187..198 then x = 6
when 95..99 then x = 7
@betawaffle
betawaffle / application.html.erb
Created August 3, 2011 15:54
Controller Ancestry
<!DOCTYPE html>
<html>
<head>
<!-- Which allows you to do cool stuff like this: -->
<%= stylesheet_link_tag *controller.ancestry %>
<%= javascript_include_tag *controller.ancestry %>
<!-- So from within BarController, the stylesheets would look like this: -->
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
#include <stdlib.h>
#include <stdio.h>
int main() {
int **end;
int *last = (int *) malloc(100 * sizeof(int *));
while (last != NULL) {
end = &last;
last = (int *) malloc(100 * sizeof(int *));
}
@betawaffle
betawaffle / missing.rb
Created August 25, 2011 12:49
Find the Missing Integer
def missing(list)
min, max, sum = list.reduce([f = list.first, f, 0]) do |(l, h, s), n|
[n < l ? n : l, n > h ? n : h, s + n]
end
max * (max + 1) / 2 - min * (min - 1) / 2 - sum
end
def method_info
required_args = obj.method(m).arity
variable_args = required_args < 0
[variable_args ? -(required_args + 1) : required_args, variable_args]
end
@betawaffle
betawaffle / performable.rb
Created September 24, 2011 13:59
Resque + ActiveRecord
module Performable
def self.included(receiver)
# TODO: Raise exception if receiver doesn't implement perform
# unless receiver.method_defined? :perform
# raise ...
# end
receiver.class_eval do
def self.perform(id)
find(id).perform
old = Resque.working.select do |w|
w.processing['run_at'].to_datetime < 5.minutes.ago
end
old.each { |w| Process.kill('USR1', w.pid) }
@betawaffle
betawaffle / pry-bench.rb
Created October 25, 2011 00:56
Pry Helpers
Pry.config.should_load_plugins = false
Pry.config.editor = proc { |file, line| "subl -w #{file}:#{line}" }
Pry.prompt = [
proc { |obj, nest_level| "#{RUBY_VERSION} (#{obj}):#{nest_level} > " },
proc { |obj, nest_level| "#{RUBY_VERSION} (#{obj}):#{nest_level} * " }
]
Pry.plugins['doc'].activate!
class BasicObject
Pry.instance_eval do
def show_lines(file, line_num, klass, num = nil)
if file =~ /(\(.*\))|<.*>/ || file == "" || file == "-e"
output.puts "Cannot find local context. Did you use `binding.pry` ?"
next
end
Pry.output.puts "\n\e[1mFrom:\e[0m #{file} @ line #{line_num} in #{klass}:\n\n"
if num
class RemoveMethodHook < Exception; end
class Module
def before_method(name, &new_method)
raise ArgumentError unless block_given?
old_method = instance_method(name) rescue nil
# new_method = Proc.new
unless old_method.nil?
define_method(name) do |*args|