Skip to content

Instantly share code, notes, and snippets.

@AliTopal89
Created April 8, 2015 13:45
Show Gist options
  • Save AliTopal89/45ac635593cd5ff141d5 to your computer and use it in GitHub Desktop.
Save AliTopal89/45ac635593cd5ff141d5 to your computer and use it in GitHub Desktop.
# 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)
numbers = [1,2,3,4,5]
numbers_all = numbers[0]*2, numbers[1]*2, numbers[2]*2, numbers[3]*2, numbers[4]*2
puts numbers_all.inspect
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
# 3.A) Create a ship object and save it to a variable.
new_ship = Ship.create(:name => "Excalibur")
# 3.B) Create two sailors that belong to the ship object you created in 3.A.
new_ship.sailors.create(:name => "Archer")
new_ship.sailors.create(:name => "Dangerzone")
# 3.C) Puts the names of all sailors that belong to the variable you created in 3.A.
new_ship.sailors.each do |x|
x.name
end
## Problem 4
=begin
Create a method that accepts 2 arguments and always returns false.
=end
def never_true(falsish_1, falsish_2)
return false
end
never_true("Randy is Batman","Robin is from Florida")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment