Skip to content

Instantly share code, notes, and snippets.

@foreverman
foreverman / bdd.vim
Created March 13, 2011 04:05 — forked from heisters/bdd.vim
" Vim functions to run RSpec and Cucumber on the current file and optionally on
" the spec/scenario under the cursor. Within the context of a Rails app, will
" prefer script/spec and script/cucumber over their system counterparts.
function! RailsScriptIfExists(name)
" Bundle exec
if isdirectory(".bundle") || (exists("b:rails_root") && isdirectory(b:rails_root . "/.bundle"))
return "bundle exec " . a:name
" Script directory
elseif exists("b:rails_root") && filereadable(b:rails_root . "/script/" . a:name)
return b:rails_root . "/script/" . a:name
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
jobs_per_process = 100
process_count = 10
User.find_in_batches(:batch_size => jobs_per_process * process_count) do |group|
batches = group.in_groups(process_count)
jobs_per_process = 100
process_count = 10
User.find_in_batches(:batch_size => jobs_per_process * process_count) do |group|
batches = group.in_groups(process_count)
Parallel.each(batches, :in_processes => process_count) do |batch|
ActiveRecord::Base.establish_connection
# Do the actual work
batch.each {|user| .. }
end
@foreverman
foreverman / gist:2607189
Created May 6, 2012 02:49
Convert time between different time zones
ruby-1.8.7-p352 :002 > #current time zone
ruby-1.8.7-p352 :003 > Time.zone
=> #<ActiveSupport::TimeZone:0x10e5d8c98 @current_period=nil, @name="UTC", @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @utc_offset=nil>
irb(main):009:0> d.created_at
=> Fri, 04 May 2012 06:57:18 UTC +00:00
irb(main):012:0> d.created_at.time_zone.name
=> "UTC"
@foreverman
foreverman / exit-codes.sh
Created May 7, 2012 14:38 — forked from defunkt/exit-codes.sh
Ruby exit codes
$ ruby -e 'exit'
$ echo $?
0
$ ruby -e 'exit 1'
$ echo $?
1
$ ruby -e 'abort "oh no"'
oh no
$ echo $?
1
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))
# a module to make insertions and updates serializable given the unique keys/fields
# a unique constraint have to be created based the keys
module Serializable
EXCEPTIONS = [
ActiveRecord::StaleObjectError, ActiveRecord::StatementInvalid,
ActiveRecord::RecordNotUnique
]
def self.included(base_class)
base_class.extend(ClassMethods)
class Integer
def sum_of_digits
#why the following inject method call works?
#[1,2].inject(0) {|object, *args| object.send('+', *args)}
to_s.split('').map(&:to_i).inject(0, &:+)
end
end
# why the following doesn't work
[12, 34].inject(0, &:sum_of_digits)