Skip to content

Instantly share code, notes, and snippets.

@chadxsmith
Created April 8, 2015 13:39
Show Gist options
  • Save chadxsmith/762b6d76dde5726e0f3c to your computer and use it in GitHub Desktop.
Save chadxsmith/762b6d76dde5726e0f3c to your computer and use it in GitHub Desktop.
Quiz
require 'pry'
# W04D03 Quiz
=begin
Your solutions to all the problems below (except for Problem 1) should be included in this file.
Assuming the database "quiz" mentioned in Problem 3 has been created, this file should be able to be executed in the command line (`ruby d03_quiz.rb`) without throwing any errors.
=end
## Problem 1
=begin
As you would with a regular homework assignment, please pull this file to your homework repository, make the necessary edits, push it, and then make a pull request.
The instructors will not provide assistance. If you run into trouble, please copy and paste the file as a public Gist (https://gist.github.com/), and share the link with the instructors as an issue in the addbass-hw repository.
=end
## Problem 2
=begin
Define a method "numbers_plus_two" such that the return value of the method below is the array [2,4,6,8,10]. Use a numerator inside the method definition.
=end
def numbers_plus_two(numbers)
new_number = numbers.map do |number|
number = number * 2
end
end
puts numbers_plus_two([1,2,3,4,5])
## Problem 3
=begin
Given the following code:
=end
require "active_record"
ActiveRecord::Base.establish_connection(
adapter: "postgresql",
host: "localhost",
database: "quiz"
)
ActiveRecord::Base.connection.tables.each do |table_name|
if ActiveRecord::Base.connection.table_exists? table_name
ActiveRecord::Migration.drop_table(table_name)
end
end
ActiveRecord::Migration.create_table :ships do |column|
column.string :name, null: false
end
ActiveRecord::Migration.create_table :sailors do |column|
column.string :name, null: false
column.belongs_to :ship
end
class Ship < ActiveRecord::Base
has_many :sailors
end
class Sailor < ActiveRecord::Base
belongs_to :ship
end
binding.pry
# 3.A) Create a ship object and save it to a variable.
ship1 = Ship.create(name:"LoveBoat")
# 3.B) Create two sailors that belong to the ship object you created in 3.A.
salior1 = ship1.sailors.create(name:"John")
salior1 = ship1.sailors.create(name:"Tim")
# 3.C) Puts the names of all sailors that belong to the variable you created in 3.A.
ship1.saliors.each do |salior|
salior.name
end
## Problem 4
=begin
Create a method that accepts 2 arguments and always returns false.
=end
def quiz (x,y,y)
return false
end
quiz("yo","yooo","yoooo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment