Skip to content

Instantly share code, notes, and snippets.

View toreriklinnerud's full-sized avatar

Tor Erik Linnerud toreriklinnerud

  • Decibel
  • New York
View GitHub Profile
# Needs Ruby 1.8.7+ for built in #group_by and #reduce
# Implementation
Product = Struct.new(:code, :name, :price) do
def line_total(quantity)
price * quantity
end
end
Pistachio 1.9 procedure
freels-mongrel
http://pauldoerwald.ca/2009/6/15/ruby-1-9-rails-and-unicode-nightmares
@toreriklinnerud
toreriklinnerud / autospork.rb
Created October 14, 2009 22:08
Start spork before autospec if not already running
#!/usr/bin/env ruby
`ps | grep /spork$ | grep -v grep`
if $?.success?
exec('autospec')
else
IO.popen('(spork&) 2>&1') do |messages|
puts(message = messages.gets) until message =~ /ready/
exec('autospec')
@toreriklinnerud
toreriklinnerud / ZippyHash.rb
Created October 21, 2009 19:53 — forked from jcf/ZippyHash.rb
using graph, common method and present in our app, but not standard in ruby/rails
def build_message
message = PORTFOLIO_FIELDS.graph{|field, _| [field, send(field)] }
self.message = message.delete_if { |_, value| value.nil? }
end
g++ -L/Users/tel/code/rubinius/vm/external_libs/llvm/Release/lib -lpthread -lffi -lm -o vm/vm vm/external_libs/libptr_array/libptr_array.a vm/external_libs/libcchash/libcchash.a vm/external_libs/libmquark/libmquark.a vm/external_libs/libtommath/libtommath.a vm/external_libs/libbstring/libbstring.a vm/external_libs/libgdtoa/libgdtoa.a vm/external_libs/onig/.libs/libonig.a vm/external_libs/libffi/.libs/libffi.a vm/external_libs/libltdl/.libs/libltdl.a vm/external_libs/libev/.libs/libev.a vm/assembler/libudis86.a vm/agent.o vm/ar.o vm/arguments.o vm/call_frame.o vm/compiled_file.o vm/config_parser.o vm/dispatch.o vm/environment.o vm/event.o vm/exception.o vm/exception_point.o vm/ffi.o vm/ffi_util.o vm/global_cache.o vm/global_lock.o vm/helpers.o vm/inline_cache.o vm/instructions.o vm/linkedlist.o vm/marshal.o vm/native_libraries.o vm/native_thread.o vm/objectmemory.o vm/ontology.o vm/oop.o vm/primitives.o vm/shared_state.o vm/signal.o vm/stack_variables.o vm/symboltable.o vm/thread_state.o vm/type_info.o vm/vir
search = 80
hash = {1 => 83, 2 => 174, 3 => 276, 4 => 388, 5 => 512}
hash.values.min do |a,b|
(a - search).abs <=> (b - search).abs
end
su postgres -c "/opt/local/lib/postgresql84/bin/pg_ctl -D ${POSTGRESQL84DATA:=/opt/local/var/db/postgresql84/defaultdb} start -l /opt/local/var/log/postgresql84/postgres.log"
count = 90
total = 100
%w(red amber green).map.with_index do |color, index|
color if ((index + 1)/ 3.0) > (count.to_f / total.to_f)
end.compact.first
@toreriklinnerud
toreriklinnerud / database_views.rb
Created July 29, 2011 16:13
Drop and recreate all views after database migrations
desc "Drop and create all views"
task 'db:reload_views' => :environment do
ActiveRecord::Migration.verbose = false
Logger.new(STDOUT).debug('Rebuilding Views...')
ActiveRecord::Migrator.down('db/views/')
ActiveRecord::Migrator.up('db/views/')
ActiveRecord::Migration.verbose = true
end
Rake::Task['db:migrate'].enhance do
@toreriklinnerud
toreriklinnerud / student_view.sql
Created August 17, 2011 17:15
Example database view
DROP VIEW IF EXIXTS student_views;
CREATE VIEW student_views AS
SELECT
students.id AS id,
students.name AS name,
teachers.name AS teacher_name,
schools.name AS school_name,
countries.name AS country_name
FROM students
LEFT JOIN teachers ON teachers.id = students.teacher_id