This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 *)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def method_info | |
required_args = obj.method(m).arity | |
variable_args = required_args < 0 | |
[variable_args ? -(required_args + 1) : required_args, variable_args] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
old = Resque.working.select do |w| | |
w.processing['run_at'].to_datetime < 5.minutes.ago | |
end | |
old.each { |w| Process.kill('USR1', w.pid) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |