Skip to content

Instantly share code, notes, and snippets.

create_table "addresses", :force => true do |t|
t.string "firstname"
t.string "lastname"
t.string "address1"
t.string "address2"
t.string "city"
t.integer "state_id"
t.string "zipcode"
t.integer "country_id"
t.string "phone"
#you can now chain operations by scope
Order.scoped_by_customer_id(12)
Order.scoped_by_customer_id(12).find(:all, :conditions => "status = 'open'")
Order.scoped_by_customer_id(12).scoped_by_status("open")
#Customer is a trillion row table
Customer.find_in_batches(:conditions => {:active => true}) do |customer_group|
customer_group.each { |customer| customer.update_account_balance! }
end
def model_for_table(table_name)
table_name.classify.constantize
end
def index
schemas = Hash.new
ActiveRecord::Base.connection.tables.each do |table|
if table != "schema_migrations"
model = model_for_table(table)
schemas[model.table_name] << model.columns.map { |c| { :name => c.name, :type => c.type } }
require 'rubygems'
require 'amatch'
require 'digest/sha1'
#data structs
REF_SHA = Digest::SHA1.hexdigest('I would much rather hear more about your whittling project')
DICT = File.read('mydict.txt').map { |x| x.chomp }
ALPHA = (('A'..'Z').to_a << ('a'..'z').to_a).flatten
HAM = Amatch::Hamming.new(REF_SHA)
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'amatch'
=> true
irb(main):003:0> str1 = 'cd36b6dc8d4ed51b36dd7fce08f500392a7fb782'
=> "cd36b6dc8d4ed51b36dd7fce08f500392a7fb782"
irb(main):004:0> str2 = '6cac827bae250971a8b1fb6e2a96676f7a077b60'
=> "6cac827bae250971a8b1fb6e2a96676f7a077b60"
irb(main):005:0> m = Amatch::Hamming.new(str1)
=> #<Amatch::Hamming:0xb79cc5cc>
capotej@P1:~ ➔ irb
irb(main):001:0> 1,3
SyntaxError: compile error
(irb):1: syntax error, unexpected ',', expecting $end
from (irb):1
irb(main):002:0> lamda do
irb(main):003:1* 1,3
irb(main):004:1> end
SyntaxError: compile error
(irb):3: syntax error, unexpected ',', expecting kEND
#cdr in ruby benchmarks
require 'benchmark'
arr = (0...1000).to_a
puts Benchmark.measure { 10000.times { arr.values_at(1..-1) } }
# 0.570000 0.010000 0.580000 ( 0.586325)
puts Benchmark.measure { 10000.times { arr.slice(1..-1) } }
# 0.020000 0.000000 0.020000 ( 0.012761)
We couldn’t find that file to show.
require 'open-uri'
puts open('http://j.mp/3nqLRU').read.match(/<text>((\d|\.|\ )*)/)[0].gsub('<text>','').split(' ').map { |x| x.split('.').map { |y| ('A'..'Z').to_a[y.to_i - 1] } }
require 'open-uri'
require 'benchmark'
def p
print '.'
end
#non threaded
u = Benchmark.measure do
open('http://www.yahoo.com').read;p