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
| defmodule VehicleMake do | |
| schema "vehicle_makes" do | |
| field :name, :string | |
| has_many :vehicle_models, VehicleModel | |
| has_many :vehicles, through: [:vehicle_models, :vehicles] | |
| end | |
| end | |
| defmodule VehicleModel do |
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
| defmodule ProductVehicle do | |
| def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do | |
| query | |
| |> join(:inner, | |
| [products_vehicles], | |
| product in assoc(products_vehicles, :product)) | |
| |> join(:inner, | |
| [products_vehicles], | |
| vehicle in assoc(products_vehicles, :vehicle)) | |
| 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
| defmodule ProductVehicle do | |
| def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do | |
| query | |
| |> join(:inner, | |
| [products_vehicles], | |
| product in assoc(products_vehicles, :product)) | |
| |> join(:inner, | |
| [products_vehicles, _product], | |
| vehicle in assoc(products_vehicles, :vehicle)) | |
| 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
| defmodule ProductVehicle do | |
| def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do | |
| query | |
| |> join(:inner, | |
| [products_vehicles], | |
| product in assoc(products_vehicles, :product)) | |
| |> join(:inner, | |
| [products_vehicles], | |
| vehicle in assoc(products_vehicles, :vehicle)) | |
| |> join(:inner, |
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
| defmodule ProductVehicle do | |
| def vehicles_that_fit_product(query \\ ProductVehicle, product_id) do | |
| query | |
| |> join(:inner, | |
| [products_vehicles], | |
| product in assoc(products_vehicles, :product)) | |
| |> join(:inner, | |
| [products_vehicles], | |
| vehicle in assoc(products_vehicles, :vehicle)) | |
| |> join(:inner, |
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 FizzBuzz | |
| def self.find_answers_for(max) | |
| self.new.find_answers_for(max) | |
| end | |
| def initialize | |
| @result = [ | |
| Proc.new do |x| x; end, | |
| Proc.new do |x| x; end, | |
| Proc.new do |x| "Fizz"; 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 FizzBuzz | |
| def self.find_answers_for(max) | |
| 1.upto(max).each do |n| | |
| result = '' | |
| result << 'Fizz' if n % 3 == 0 | |
| result << 'Buzz' if n % 5 == 0 | |
| result = n if result.empty? | |
| result | |
| 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 FizzBuzz | |
| def self.find_answers_for(max) | |
| self.new.find_answers_for(max) | |
| end | |
| def find_answers_for(max) | |
| 1.upto(max).map do |current| | |
| [find_result_hash(current), current] | |
| .find { |result| result != '' } | |
| 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
| # Run if necessary: | |
| # RubyVM::InstructionSequence.compile_option = { | |
| # tailcall_optimization: true, | |
| # trace_instruction: false | |
| # } | |
| class FizzBuzz | |
| def self.find_answers_for(max) | |
| self.new(max).find_answers_for([], 1) | |
| 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
| defmodule MagicSquare do | |
| @moduledoc """ | |
| Today's daily programmer challenge is to test a magic square. This challenge | |
| is stolen verbatim from r/dailyprogrammer. A 3x3 magic square is a 3x3 grid | |
| of the numbers 1-9 such that each row, column, and major diagonal adds up to | |
| 15. Here's an example: | |
| ``` | |
| 8 1 6 | |
| 3 5 7 | |
| 4 9 2 |