Skip to content

Instantly share code, notes, and snippets.

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
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
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) }
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
# 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
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>]>
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
# 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
# - 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 >
array = [[1],[2],[3]]
# 1 will it error? / name that error!
# 1a.
array.first + 1
# 1b.
array.first.first + 2
# 1c.