This file contains 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 'benchmark' | |
TEN = 10 | |
ONE_HUNDRED = 100 | |
ONE_THOUSAND = 1000 | |
TEN_THOUSAND = 10000 | |
ONE_HUNDRED_THOUSAND = 100000 | |
ONE_MILLION = 1000000 | |
TEN_MILLION = 10000000 | |
ONE_HUNDRED_MILLION = 100000000 |
This file contains 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
TEN = 10 | |
ONE_HUNDRED = 100 | |
ONE_THOUSAND = 1000 | |
TEN_THOUSAND = 10000 | |
ONE_HUNDRED_THOUSAND = 100000 | |
ONE_MILLION = 1000000 | |
TEN_MILLION = 10000000 | |
ONE_HUNDRED_MILLION = 100000000 |
This file contains 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 generate_ten_random_numbers | |
(0...TEN).map { |generator| rand(TEN) } | |
end | |
def generate_one_hundred_random_numbers | |
(0...ONE_HUNDRED).map { |generator| rand(ONE_HUNDRED) } | |
end | |
def generate_one_thousand_random_numbers | |
(0...ONE_THOUSAND).map { |generator| rand(ONE_THOUSAND) } |
This file contains 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
Benchmark.bm do |test| | |
ten_random_numbers = generate_ten_random_numbers | |
test.report("10 test: ") {ten_random_numbers.sort!} | |
puts "top five: #{ten_random_numbers[0..4]}\n\n" | |
one_hundred_random_numbers = generate_one_hundred_random_numbers | |
test.report("100 test: ") {one_hundred_random_numbers.sort!} | |
puts "top five: #{one_hundred_random_numbers[0..4]}\n\n" | |
one_thousand_random_numbers = generate_one_thousand_random_numbers |
This file contains 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
# Return an array of length n_simulations. | |
# Each value is a random number between 0 and @n_doors-1. | |
# The value represents the door the prize is behind | |
# or the door the contestant guessed. | |
@n_doors = 3 | |
def simulate_prizedoors(n_simulations) | |
(0...n_simulations).map {rand(@n_doors)} | |
end |
This file contains 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
candidate = FactoryGirl.create :candidate, :with_many_voters | |
# => <Candidate id: 1534, position_running_for: "President", first_name: "Rudolph", | |
# last_name: "Carter", personality_type: "Brave, Hasty", | |
# bio: "Ut molestiae maiores non corporis. Voluptatem eum ...", political_views: nil, national_candidate: true> | |
candidate.votes | |
# => #<ActiveRecord::Associations::CollectionProxy | |
# [#<Vote id: 1544, voter_id: 556, candidate_id: 1534>, | |
# #<Vote id: 1545, voter_id: 557, candidate_id: 1534>, ... | |
# #<Vote id: 1553, voter_id: 565, candidate_id: 1534>]> |
This file contains 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 MakeVoterPolymorphic < ActiveRecord::Migration | |
def change | |
add_column :voters, :voterable_id, :integer | |
add_column :voters, :voterable_type, :string | |
end | |
end | |
# you could also use a shortened syntax that would do the same thing | |
# and add these two columns to the table |
This file contains 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
# Voter, the polymorphic model | |
class Voter < ActiveRecord::Base | |
belongs_to :voterable, polymorphic: true | |
has_many :votes | |
has_many :candidates, through: :votes | |
end | |
# Citizen, has one voter as voterable | |
# this is a new model where many of the attributes |
This file contains 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
# - create a candidate | |
# -------------------- | |
obama = Candidate.create(first_name: "Barack", last_name: "Obama", birthdate:"08-04-1961", | |
position_running_for: "President", national_candidate: true) | |
# (0.1ms) begin transaction | |
# SQL (0.3ms) INSERT INTO "candidates" ("first_name", "last_name", "birthdate", "position_running_for", "national_candidate") VALUES (?, ?, ?, ?, ?) [["first_name", "Barack"], ["last_name", "Obama"], ["birthdate", "1961-04-08"], ["position_running_for", "President"], ["national_candidate", "t"]] | |
# (1.4ms) commit transaction | |
# => #<Candidate id: 1544, position_running_for: "President", first_name: "Barack", last_name: "Obama", | |
# birthdate: "1961-04-08", national_candidate: true > |
This file contains 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
array = [[1],[2],[3]] | |
# 1 will it error? / name that error! | |
# 1a. | |
array.first + 1 | |
# 1b. | |
array.first.first + 2 | |
# 1c. |
OlderNewer