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 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) |
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
# 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) |
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 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)) |
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
$ ruby -e 'exit' | |
$ echo $? | |
0 | |
$ ruby -e 'exit 1' | |
$ echo $? | |
1 | |
$ ruby -e 'abort "oh no"' | |
oh no | |
$ echo $? | |
1 |
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
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" |
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
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 |
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
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) |
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
" 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 |