This exercise is intended to help you assess your progress with the concepts and techniques we've covered during the week.
For these questions, write a short snippet of code that meets the requirement. In cases where the question mentions a "given" data value, use the variable given to refer to it (instead of re-writing the information).
1. Define a class called PizzaOven which has a method cook_pizza which returns the string "mmm 'za".
class PizzaOven
def cook_pizza
"/"mmm /'za/""
end
end2. 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
end
end3. Given an array of the numbers [1,2,3,4,5], how would you create a new array of all elements doubled? How would you return an array of all odd elements?
array = [1, 2, 3, 4, 5]
#doubles stored in array
doubled = array.map do |num|
num * 2
end
#new array of all odds
odds = array.select do |num|
num.odd?
endgit init
5. 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
def test_if_pizza_is_tasty
my_pizza = Pizza.new
assert my_pizza.is_tasty?
end
end6. 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.
def test_pizza_has_style_options
my_pizza = Pizza.new
styles = ["supreme", "mediterranian", "cheese"]
assert_includes styles, my_pizza.style
endgit add git commit -m
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.
class Student
attr_reader :attitude
def initialize
@attitude = cheerful
end
end8. 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.
This question could not be more on point.
class Student
attr_reader :attitude
def initialize(attitude = "cheerful")
@attitude = attitude
end
def assign_homework
if @attitude == "perturbed"
@attitude = "dazed"
elsif @attitude == "dubious"
@attitude = "perturbed"
elsif @attitude == "cheerful"
end
end
end9. 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:
Don't Understand the question well enough to take action.
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"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")
=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"def print_variables(x)
puts "x: #{x}"
puts "b: #{b}"
end
def b
12
end
a = 4
print_variables(a)
=> x: 4 #<----- ANSWER
=> b: 1212. 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.
File.open("~/Documents/pizza.txt", "r").each_line do |line| #<--- "r" for read file
puts line
end13. 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.
NO IDEA
14. 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
def test_if_corgi_with_no_arguements
corgi1 = Corgi.New
assert_instance_of Corgi
end
- A Corgi can be assigned a name
def test_if_corgi_can_have_name
corgi1 = Corgi.New
assert_if_equal Corgi.new(name), "Jasper"
end
- A Corgi can be asked for its name
def test_if_corgi_can_have_name
corgi1 = Corgi.New
assert corgi1.name
end
- A Corgi can be asked for its posture, which should default to "standing"
class Corgi
attr_reader :posture
def initialize(posture)
@posture = "standing"
end
end
def test_posture
corgi1 = Corgi.New
assert_if_equal Corgi.new(posture), "standing"
end
- A Corgi can be asked to lie down, which should change its posture to "laying"
NOT SURE