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).
Use single (`) and triple backticks (```) to container code snippets.
- Define a class called
PizzaOven
which has a methodcook_pizza
which returns the string"mmm 'za"
.
class PizzaOven
def cook_pizza
"mmm 'za"
end
end
- Define a class called
Student
which is instantiated with a "name" value and which has a methodname
that returns this value
class Student
attr_reader :name
def initialize(name)
@name = name
end
end
- Given an array of the numbers
[1,2,3,4,5]
, find the sum of the doubles of all the numbers
summed = given.map { |n| n * 2 }
summed.reduce(:+)
- Give the command to create a new Git repository in a directory on your machine
git init
- Given a hypothetical
Pizza
class which has an instance methodis_tasty?
that always returns true, write a simple Minitest test that tests this behavior.
def test_does_tasty_return_true
taco_pizza = Pizza.new
assert taco_pizza.tasty?
end
- ?Suppose the
Pizza
class also has a methodstyle
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_does_style_return_from_given_list
fresh_pizza = Pizza.new
assert_equal [some way to allow a range of choices], fresh_pizza.style
end
- Give the Git commands needed to stage and then commit a set of changes to a file
git add .
git commit -m "commit message in present tense"
- Define a
Student
class which, when created, has anattitude
attribute.attitude
should start out with the value "cheerful", and theStudent
class should provide a "reader" method that allows us to access the value of itsattitude
.
class Student
attr_reader :attitude
def initialize(attitude = "cheerful")
@attitude = attitude
end
end
- Additionally, add an
assign_homework
method toStudent
. Whenassigned_homework
is invoked, if the student'sattitude
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.
class Student
attr_reader :attitude
def initialize(attitude = "cheerful")
@attitude = attitude
end
def assign_homework
if attitude == "cheerful"
attitude = "dubious"
elsif attitude == "dubious"
attitude = "perturbed"
elsif attitude == "perturbed"
attitude = "dazed"
else
attitude = "dazed"
end
end
end
- Building on the
Student
class from the previous example, update theassign_homework
method to accept an argument. The argument will be aString
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"
class Student
attr_reader :attitude, :task
def initialize(attitude = "cheerful")
@attitude = attitude
@task = task
end
def assign_homework(task)
task_list = []
if attitude == "cheerful"
attitude = "dubious"
task_list << task
elsif attitude == "dubious"
attitude = "perturbed"
task_list << task
elsif attitude == "perturbed"
attitude = "dazed"
task_list << task
else
attitude = "dazed"
task_list << task
end
task_list
end
def assignments
task_list.join(", ")
end
end
- 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]
# YOUR CODE HERE
=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
MY CODE: puts students.assignments.flatten.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"
4 is passed to the print_variables
argument, which then looks for b, first as a local variable and then as a sibling method. since b returns 12, it is passed as well.
- ?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").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.
file_to_work_with = File.open("~/Documents/pizza.txt", "r")
line_count = file_to_work_with.readlines
new_file = File.open("~/Documents/line_count.txt", "w").write(line_count)
- 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 setup
donald_the_corgi = Corgi.new
end
def test_can_corgi_be_created
assert_instance_of Corgi, donald_the_corgi
end
def test_can_corgi_get_name
assert_equal "Donald", donald_the_corgi.name
end
def test_can_corgi_be_questioned_about_name
assert_equal "Donald", donald_the_corgi.ask_name
end
def test_can_corgi_be_questioned_about_posture
assert_equal "standing", donald_the_corgi.ask_posture
end
def test_can_corgi_be_asked_to_lie_down
assert_equal "laying", donald_the_corgi.lie_down
end