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 Carrot < ActiveRecord::Base | |
belongs_to :bunny | |
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 CreateCarrots < ActiveRecord::Migration | |
def change | |
create_table :carrots do |t| | |
t.string :color | |
t.timestamps | |
end | |
end | |
end | |
class CreateBunnies < ActiveRecord::Migration |
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 Carrot < ActiveRecord::Base | |
has_many :communities | |
has_many :rabbits, :through => :communities | |
end | |
class Bunny < ActiveRecord::Base | |
has_many :communities | |
has_many :carrots, :through => :communities | |
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
2.0.0-p247 :042 > bunny = Bunny.first | |
Bunny Load (0.2ms) SELECT "bunnies".* FROM "bunnies" ORDER BY "bunnies"."id" ASC LIMIT 1 | |
=> #<Bunny id: 9, name: "Jasper", created_at: "2014-02-09 15:01:25", updated_at: "2014-02-09 15:01:25"> | |
2.0.0-p247 :043 > bunny.carrot_ids | |
(0.1ms) SELECT "carrots".id FROM "carrots" INNER JOIN "communities" ON "carrots"."id" = "communities"."carrot_id" WHERE "communities"."bunny_id" = ? [["bunny_id", 9]] | |
=> [] | |
2.0.0-p247 :044 > bunny.carrot_ids = [9, 10] | |
Carrot Load (0.3ms) SELECT "carrots".* FROM "carrots" WHERE "carrots"."id" IN (9, 10) |
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
2.0.0-p247 :053 > carrot = Carrot.first | |
Carrot Load (0.1ms) SELECT "carrots".* FROM "carrots" ORDER BY "carrots"."id" ASC LIMIT 1 | |
=> #<Carrot id: 9, color: "green", created_at: "2014-02-09 15:02:04", updated_at: "2014-02-09 15:02:04"> | |
2.0.0-p247 :054 > carrot.bunny_ids | |
(0.1ms) SELECT "bunnies".id FROM "bunnies" INNER JOIN "communities" ON "bunnies"."id" = "communities"."bunny_id" WHERE "communities"."carrot_id" = ? [["carrot_id", 9]] | |
=> [9] | |
2.0.0-p247 :058 > carrot.bunnies.collect{|bunny| bunny.name} | |
=> ["Jasper"] |
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
bunny2 = Bunny.find_by name: "Hakeem" | |
Bunny Load (0.2ms) SELECT "bunnies".* FROM "bunnies" WHERE "bunnies"."name" = 'Hakeem' LIMIT 1 | |
=> #<Bunny id: 13, name: "Hakeem", created_at: "2014-02-09 15:01:53", updated_at: "2014-02-09 15:01:53"> | |
2.0.0-p247 :091 > bunny2.carrot_ids | |
(0.1ms) SELECT "carrots".id FROM "carrots" INNER JOIN "communities" ON "carrots"."id" = "communities"."carrot_id" WHERE "communities"."bunny_id" = ? [["bunny_id", 13]] | |
=> [] | |
2.0.0-p247 :092 > bunny2.carrot_ids = [9,10] | |
Carrot Load (0.3ms) SELECT "carrots".* FROM "carrots" WHERE "carrots"."id" IN (9, 10) |
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
stock_prices = [1, 5, 8, 3, 13, 18, 11, 44] | |
def max_profit stock_prices | |
profit_so_far = 0 | |
stock_prices.each_with_index do |buy_price, buy_time| | |
stock_prices.each_with_index do |sell_price, sell_time| | |
current_profit = buy_price - sell_price | |
if current_profit > profit_so_far && buy_time < sell_time | |
profit_so_far = current_profit | |
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 Node | |
attr_reader :content | |
attr_accessor :neighbor | |
def initialize(x) | |
@content = x | |
@neighbor = nil | |
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 LinkedList | |
attr_reader :root | |
def initialize(node) | |
@root = node | |
end | |
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 LinkedList | |
attr_reader :root | |
def initialize node | |
@root = node | |
end | |
def add x | |
current = root |