Created
September 16, 2015 14:31
-
-
Save TylerRockwell/4099ec73349ee0f1759e to your computer and use it in GitHub Desktop.
Name Split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require 'minitest/pride' | |
# Write two methods: | |
# | |
# * `first_name`: given a name in string, return the first name. | |
# * `last_name`: given a name in string, return the last name. | |
# WRITE YOUR CODE HERE. | |
class StringSplitChallenge < MiniTest::Test | |
def test_first_name | |
assert_equal "Mason", first_name("Mason Matthews") | |
end | |
def test_last_name | |
assert_equal "Matthews", last_name("Mason Matthews") | |
end | |
def test_one_word_name | |
assert_equal "deadmou5", first_name("deadmou5") | |
assert_equal "", last_name("deadmou5") | |
end | |
def test_three_word_name | |
assert_equal "John Quincy", first_name("John Quincy Adams") | |
assert_equal "Adams", last_name("John Quincy Adams") | |
end | |
def test_no_word_name | |
assert_equal "", first_name("") | |
assert_equal "", last_name("") | |
end | |
def test_nil_name | |
assert_equal "", first_name(nil) | |
assert_equal "", last_name(nil) | |
end | |
end | |
def first_name(string) | |
if string == nil || string == "" | |
return "" | |
elsif string.include?(" ") | |
names = string.split(" ") | |
names[0] | |
else | |
return string | |
end | |
end | |
def last_name(string) | |
if string == nil || string == "" | |
return "" | |
elsif string.include?(" ") | |
names = string.split(" ") | |
names[-1] | |
else | |
return "" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment