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).
def print_variables(x)
puts "x: #{x}"
puts "b: #{b}"
end
def b
12
end
a = 4
print_variables(a)
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.
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"
Given an array of words ["dog", "cat", "gerbil", "cat", "hamster", "rabbit", "rabbit"]
,
create a Hash containing the individual words as keys and the number of times
the word appears in the list as values. That is:
{"dog" => 1, "cat" => 2, "gerbil" => 1, "hamster" => 1, "rabbit" => 2}
Given a text file located at "~/Documents/pizza.txt"
, write code to read the
file from the filesystem, then process the file's lines so that:
- Even lines go into an array called
even
- Odd lines go into an array called
odd
(Assume the first line is numbered 0
, and is thus even)
Given the following code, draw a simple diagram representing the stack frames that the program will generate as it is run. In order to show change in the stack over time, you may need to re-copy the lower frames into a new diagram.
def wrap_it(x)
"<<<" + x + ">>>"
end
def string_it(x)
x.to_s
end
def churn_it(x)
wrap_it(string_it(x))
end
churn_it(10)