Created
April 8, 2015 13:52
-
-
Save michaelkendall/d192e0215e6c91b084f9 to your computer and use it in GitHub Desktop.
Quiz w04d03
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
# 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 "doubler" 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 doubler(numbers) | |
numbers *2 each.do |x| | |
end | |
puts doubler([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 | |
# 3.A) Create a ship object and save it to a variable. | |
new_ship= Ship.create(:name => "Titanic7") | |
# 3.B) Create two sailors that belong to the ship object you created in 3.A. | |
new_sailor1 = Sailor.create(:name => "Ryan",:ship_id =>new_ship.id) | |
new_sailor2 = Sailor.create(:name => "Odis", :ship_id =>new_ship.id) | |
# 3.C) Puts the names of all sailors that belong to the variable you created in 3.A. | |
new_ship.sailors.each do |x| | |
puts x.name | |
end | |
puts new_sailor1.name | |
puts new_sailor2.name | |
## Problem 4 | |
def two bb,cc | |
return false | |
end | |
binding.pry | |
=begin | |
Create a method that accepts 2 arguments and always returns false. | |
d | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
quiz submission