Skip to content

Instantly share code, notes, and snippets.

@jmmastey
Created June 9, 2014 19:58
Show Gist options
  • Save jmmastey/656091859942f5776816 to your computer and use it in GitHub Desktop.
Save jmmastey/656091859942f5776816 to your computer and use it in GitHub Desktop.

Robot Name

Write a program that manages robot factory settings.

When robots come off the factory floor, they have no name.

The first time you boot them up, a random name is generated, such as RX837 or BC811.

Every once in a while we need to reset a robot to its factory settings, which means that their name gets wiped. The next time you ask, it gets a new name.

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • Random names means a risk of collisions. Make sure the same name is never used twice. Feel free to introduce additional tests.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

Source

A debugging session with Paul Blackwell at gSchool. view source

require 'minitest/autorun'
require_relative 'robot'
class RobotTest < MiniTest::Unit::TestCase
def test_has_name
assert_match /\w{2}\d{3}/, Robot.new.name
end
def test_name_sticks
robot = Robot.new
robot.name
assert_equal robot.name, robot.name
end
def test_different_robots_have_different_names
assert Robot.new.name != Robot.new.name
end
def test_reset_name
robot = Robot.new
name = robot.name
robot.reset
name2 = robot.name
assert name != name2
assert_match /\w{2}\d{3}/, name2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment