-
What do we mean when we talk about a variable's "scope"?
-
a variable's scope is defined by where it is accessed, such as on a global, instance, or class level.
-
Given the following code, what does the program output?
def combine_variables(x)
puts "inner x is: #{x}"
puts "and outer b is: #{b}"
end
def b
12
end
a = 4
combine_variables(a)
This program outputs the method call at the bottom, "combine_variables(a)" which works done the rest of the code where a is 4. It will return "inner x is 4 \n and outer b is 12
- What is the value of
hero
after this code has executed?
new_creatures = ["IntrepidDecomposedCyclist", "Dragon"]
villain = "AssistantViceTentacleAndOmbudsman"
hero = "who knows"
new_creatures.each do |villain|
hero = "Dwemthy"
puts "this time the villain is #{villain} and the hero is #{hero}"
end
after code execution, the hero is "Dwemthy"
-
What is a "handle" in terms of File I/O?
-
a file handle is a method of interaction with a file
-
What is the difference between "w", "r", and "a" modes in File I/O?
-
when opening a file you can use options "w" = write, "r" = read, "a" = write (at the end of the file)
-
Imagine we're writing software to model a Corgi dog. You don't need to write the implementation, but write a test for each of the following features:
-
A Corgi has a name.
-
A Corgi has an age.
-
A Corgi can lie down.
-
A Corgi can stand up.
-
A Corgi cannot lie down when already lying down.
-
A Corgi cannot stand when it is already standing.
-
A Corgi starts hungry.
-
A Corgi can eat, but only when it is hungry.
-
A Corgi is no longer hungry when it has eaten three times.
class CorgiTest < Minitest::test
def test_it_has_a_name
corgi = Corgi.new
assert_equal "Johnson", corgi.name
end
def test_it_has_an_age
corgi = Corgi.new
assert_equal 87, corgi.age
end
def test_it_can_lie_down
corgi = Corgi.new
assert_equal true, corgi.lying_down?
end
def test_it_can_stand_up
corgi = Corgi.new
corgi.stand_up
assert_equal false, corgi.lying_down?
end
def test_it_cannot_lie_down_when_already_lying_down
corgi = Corgi.new
corgi.lie_down
assert_equal "corgi is already laying down.", corgi.lie_down
end
def test_it_cannot_stand_up_when_already_standing
corgi = Corgi.new
corgi.stand_up
assert_equal false, corgi.lying_down?
corgi.stand_up
assert_equal "corgi is already standing up.", corgi.stand_up
end
def test_it_gets_hungry
corgi = Corgi.new
assert_equal true, corgi.hungry?
end
def test_it_can_eat_but_only_when_hungry
corgi = Corgi.new
assert_equal "corgi cannot eat unless it is hungry.", corgi.eats
end
def test_it_can_eat_but_only_when_hungry
corgi = Corgi.new
corgi.hungry?
assert_equal "corgi ate food.", corgi.eats
end
def test_it_is_no_longer_hungry
corgi = Corgi.new
corgi.hungry?
assert_equal true, corgi.hungry?
corgi.eats * 3
assert_equal false, corgi.hungry?
end
end