Define a class called PizzaOven which has a method cook_pizza which returns the string "mmm 'za".
class PizzaOven
def cook_pizza
"mm za"
end
end
Define a class called Student which is instantiated with a “name” value and which has a method name that returns this value
class Student
def initialize(name)
@name = name
end
def name
@name
name
end
Given an array of the numbers [1,2,3,4,5], find the sum of the doubles of all the numbers
sum = 0
numbers = [1,2,3,4,5]
numbers.each do |number|
sum += number * 2
end
Give the command to create a new Git repository in a directory on your machine
git init
git remote add origin github_url
git push -u origin
Pizza
Given a hypothetical Pizza class which has an instance method is_tasty? that always returns true, write a simple Minitest test that tests this behavior.
class PizzaTest < Minitest::Test
def test_is_tasty_always_true
pizza_1 = Pizza.new
assert_equal true, pizza_1.is_tasty?
end
Suppose the Pizza class also has a method style which randomly returns one of: "supreme", "mediterranean", or "cheese". Write a test that confirms that the returned pizza style is within this list.
class PizzaTest < Minitest::Test
def test_pizza_is_one_of_3_styles
pizza_1 = Pizza.new
assert_equal "supreme" || "mediterranean" || "cheese", pizza_1.style
end
Give the Git commands needed to stage and then commit a set of changes to a file
git add .
git commit
Student
Define a Student class which, when created, has an attitude attribute. attitude should start out with the value “cheerful”, and the Student class should provide a “reader” method that allows us to access the value of its attitude.
Additionally, add an assign_homework method to Student. When assigned_homework is invoked, if the student’s attitude is "cheerful", it should become "dubious". If the value is currently "dubious" it should become "perturbed". If the value is currently "perturbed", it should become "dazed". Assigning homework to a "dazed" student has no effect.
Building on the Student class from the previous example, update the assign_homework method to accept an argument. The argument will be a String containing a short description of the assignment. For example we might use it like this:
s = Student.new s.assign_homework("Write a linked list") Then, add an assignments method to Student. assignments should return a list of all the assignments that have been given, separated by a comma and a space. For example:
s = Student.new s.attitude => "cheerful" s.assign_homework("write a linked list") s.attitude => "dubious" s.assign_homework("write a BST") s.attitude => "perturbed" s.assignments => "write a linked list, write a BST" Given an array of 3 Student instances, generate a new string of all of their assignments For example:
s1 = Student.new s2 = Student.new s3 = Student.new
s1.assign_homework("linked list") s1.assign_homework("sorting algos")
s2.assign_homework("write a c compiler") s2.assign_homework("write a pacman game")
s3.assign_homework("headcount") s3.assign_homework("sales engine")
students = [s1,s2,s3]
=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
class Student
attr_accessor :attitude,
:homework_list
def initialize(attitude = "cheerful")
@attitude = attitude
@homework_list = []
end
def assign_homework(homework)
@homework_list << homework
if @homework_list.length == 1
@attitude = "dubious"
elsif @homework_list.length == 2
@attitude = "perturbed"
elsif @homework_list.length >= 3
@attitude = "dazed"
end
end
end
nick = Student.new
jack = Student.new
danny = Student.new
nick.assign_homework("linked list")
nick.assign_homework("sorting algos")
jack.assign_homework("write a c compiler")
jack.assign_homework("write a pacman game")
danny.assign_homework("headcount")
danny.assign_homework("sales engine")
students = [nick, jack, danny]
homework = students.map do |student|
student.homework_list
end
print homework.join(", ")
What does the following code output?
def print_variables(x) puts "x: #{x}" puts "b: #{b}" end
def b 12 end
a = 4 print_variables(a)
x: 4
b: 12
Working with files: given a text file located at "~/Documents/pizza.txt", write code to read the file from the filesystem and print each line one at a time.
I'm still pretty shakey on this
puts File.open("~/documents/pizza.txt).readlines
???
Writing Files: given a text file located at "/Documents/pizza.txt", write code to read the file from the filesystem, then write a new file at "/Documents/line_count.txt" containing the number of lines in the original file.
Imagine a simple ruby class designed to represent a Corgi dog. Write a test for each of the following features:
A Corgi can be created with no arguments A Corgi can be assigned a name A Corgi can be asked for its name A Corgi can be asked for its posture, which should default to “standing” A Corgi can be asked to lie down, which should change its posture to “laying”
def test_can_be_created_with_no_arguments
assert Corgi.new
end
def test_can_be_assigned_a_name
corgi_1 = Corgi.new("Sputnik)
assert_equal "Sputnik", corgi_1.name
end
def test_corgi_posture
corgi_1 = Corgi.new("Mir")
assert_equal "standing", corgi_1.posture
end
def test_corgi_lay_down
corgi_1 = Corgi.new("Voyager")
corgi_1.lie_down
assert_equal "laying", corgi_1.posture
end