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
| # 0. Make sure you have Ruby 1.9.3 installed, and optionally RVM and PostgreSQL | |
| # 0.2 If you are on the Mac, make sure you have a c compiler by installing XCode Command Line Tools or gcc4.2 with homebrew | |
| # https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers | |
| # 0.5 Make sure you have bundler version ~> 1.2 as Rails depends on it | |
| gem install bundler | |
| # 1. Get edge Rails source (master branch) | |
| git clone https://github.com/rails/rails.git |
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 counts_from_dice(dice) | |
| dice.inject({}) do |hash, value| | |
| hash[value] ||= 0 | |
| hash[value] += 1 | |
| hash | |
| end | |
| end | |
| def score(dice) | |
| counts_from_dice(dice).inject(0) do |sum, (number, 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
| # Traditional idiom | |
| total = 1 | |
| [2, 3, 5].each do |value| | |
| total = total * value | |
| end | |
| puts total | |
| # Ruby idiom | |
| puts [2, 3, 5].inject { |mult, value| mult * value } |
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 NullifyTextAttributes | |
| def self.included(base) | |
| base.class_eval do | |
| before_validation :nullify_text_attributes | |
| private | |
| def nullify_text_attributes | |
| self.class.columns.select { |c| [:text, :string].include?(c.type) }.each do |column| | |
| send("#{column.name}=", nil) if read_attribute(column.name).blank? | |
| 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
| class ImportLegacyDevices < ActiveRecord::Migration | |
| def self.up | |
| return unless Rails.env.production? | |
| legacy_devices.each do |device_id, issue| | |
| if device = Device.find_by_hardware_id(device_id) | |
| unless InclusiveIssue.exists?(:issue_id => issue.id, :device_id => device.id) | |
| InclusiveIssue.create!( | |
| :issue => issue, | |
| :device => device, |
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
| require 'rubygems' | |
| require 'debug_log' | |
| my_list = [:a, :b, :c] | |
| my_number = 5 | |
| debug.log("after variable initialization", "my_number", "my_list") | |
| # => DebugLog | after variable initialization | my_number="5" (Fixnum), my_list="[:a, :b, :c]" (Array) | /Users/peter/tmp/debug-example.rb:6:in `foo' |
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
| # In spec_helper.rb or test_helper.rb: | |
| Mocha::Configuration.prevent(:stubbing_non_existent_method) | |
| module MochaExtensions | |
| module ObjectMethods | |
| def expects_message(*args, &block) | |
| Mocha::Configuration.allow(:stubbing_non_existent_method) { expects(*args, &block) } | |
| end | |
| def stubs_message(*args, &block) |
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
| # Notes on migrating from the RSpec builtin mocking framework to the Mocha framework | |
| # 1. Comment in this line in spec_helper.rb: | |
| config.mock_with :mocha | |
| # 2. Specify your gem dependency (optional) in config/environments/test.rb: | |
| config.gem "mocha", :version => "0.9.8" | |
| # 3. Use query-replace to do most but not all of the DSL conversion | |
| # should_receive -> expects |
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
| -- Order by one indexed column (FAST) | |
| newsdesk_production=# explain analyze select * from pressreleases order by published_at DESC limit 100; | |
| QUERY PLAN | |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| Limit (cost=0.00..249.91 rows=100 width=1207) (actual time=26.070..716.453 rows=100 loops=1) | |
| -> Index Scan Backward using pressreleases_published_at_index on pressreleases (cost=0.00..964766.62 rows=386042 width=1207) (actual time=26.067..716.343 rows=100 loops=1) | |
| Total runtime: 716.709 ms | |
| (3 rows) | |
| - Order by two separately indexed columns (SLOW) |
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
| # => SELECT "posts".* FROM "posts" ORDER BY published DESC, id DESC LIMIT 1 | |
| Post.send(:with_scope, {:find => {:order => "id DESC"}}) do | |
| Post.first(:order => "published DESC") | |
| end |