Skip to content

Commit

Permalink
Complete TheOdinProject#7 and TheOdinProject#8, hashes and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JCFarrow committed Jun 8, 2022
1 parent a5d223a commit 32c174b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 42 deletions.
13 changes: 11 additions & 2 deletions ruby_basics/7_hashes/exercises/hash_exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,47 @@ def create_favorite_hash(color, number)
# return a hash with the following key/value pairs:
# key of color (as a symbol) with value of the color argument
# key of number (as a symbol) with the value of the number argument
{
color: color,
number: number
}
end

def favorite_color(favorite_list)
# return the value of the color key
favorite_list[:color]
end

def favorite_number(favorite_list)
# use #fetch to return the value of the number key or 42 if the key is not found
favorite_list.fetch(:number, 42)
end

def update_favorite_movie(favorite_list, movie)
# Step 1: add/update the key of movie (as a symbol)

favorite_list[:movie] = movie
# Step 2: return the hash (because Step 1 returns the value of the movie key)
favorite_list
end

def remove_favorite_number(favorite_list)
# Step 1: delete the number data

favorite_list.delete(:number)
# Step 2: return the hash (because Step 1 returns the value of the number key)
favorite_list
end

def favorite_categories(favorite_list)
# return the keys of favorite_list
favorite_list.keys
end

def favorite_items(favorite_list)
# return the values of favorite_list
favorite_list.values
end

def merge_favorites(original_list, additional_list)
# merge the two hashes: original_list and additional_list
original_list.merge(additional_list)
end
38 changes: 19 additions & 19 deletions ruby_basics/7_hashes/spec/hash_exercises_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

# remove the 'x' from the line below to unskip the test
xit 'returns result with an array and hash' do
it 'returns result with an array and hash' do
favorite = create_favorite_hash(['orange', 'green'], { lucky: 7 })
result = { color: ['orange', 'green'], number: { lucky: 7 } }
expect(favorite).to eq(result)
Expand All @@ -20,49 +20,49 @@

describe 'favorite color exercise' do

xit 'returns a string' do
it 'returns a string' do
my_favorites = { color: 'blue', number: 65 }
expect(favorite_color(my_favorites)).to eq('blue')
end

xit 'returns an array' do
it 'returns an array' do
my_favorites = { color: ['orange', 'green'], number: { lucky: 7 } }
expect(favorite_color(my_favorites)).to eq(['orange', 'green'])
end

xit 'returns nil when the key is not found' do
it 'returns nil when the key is not found' do
my_favorites = { number: 21, movie: 'Avengers: Endgame' }
expect(favorite_color(my_favorites)).to eq(nil)
end
end

describe 'favorite number exercise' do

xit 'returns an integer' do
it 'returns an integer' do
my_favorites = { color: 'blue', number: 65 }
expect(favorite_number(my_favorites)).to eq(65)
end

xit 'returns a hash' do
it 'returns a hash' do
my_favorites = { color: ['orange', 'green'], number: { lucky: 7 } }
expect(favorite_number(my_favorites)).to eq({ lucky: 7 })
end

xit 'returns the default number when the key is not found' do
it 'returns the default number when the key is not found' do
my_favorites = { color: ['orange', 'green'], movie: 'Avengers: Endgame' }
expect(favorite_number(my_favorites)).to eq(42)
end
end

describe 'update favorite movie exercise' do

xit 'returns hash with a new key/value pair when not included' do
it 'returns hash with a new key/value pair when not included' do
my_favorites = { color: 'blue', number: 65 }
result = { color: 'blue', number: 65, movie: 'Avengers: Endgame' }
expect(update_favorite_movie(my_favorites, 'Avengers: Endgame')).to eq(result)
end

xit 'returns hash with an updated key/value pair when included' do
it 'returns hash with an updated key/value pair when included' do
my_favorites = { color: 'emerald green', movie: 'Avengers: Endgame' }
result = { color: 'emerald green', movie: 'Avengers: Infinity War' }
expect(update_favorite_movie(my_favorites, 'Avengers: Infinity War')).to eq(result)
Expand All @@ -71,66 +71,66 @@

describe 'remove favorite number exercise' do

xit 'returns hash without key/value pair when included' do
it 'returns hash without key/value pair when included' do
my_favorites = { color: 'blue', number: 65, movie: 'Avengers: Endgame' }
result = { color: 'blue', movie: 'Avengers: Endgame' }
expect(remove_favorite_number(my_favorites)).to eq(result)
end

xit 'returns hash when key/value pair is not included' do
it 'returns hash when key/value pair is not included' do
my_favorites = { color: 'blue', movie: 'Avengers: Endgame' }
expect(remove_favorite_number(my_favorites)).to eq(my_favorites)
end
end

describe 'favorite categories exercise' do

xit 'returns an array of the keys' do
it 'returns an array of the keys' do
my_favorites = { color: 'blue', number: 65, movie: 'Avengers: Endgame' }
expect(favorite_categories(my_favorites)).to eq([:color, :number, :movie])
end

xit 'returns an empty array when hash is empty' do
it 'returns an empty array when hash is empty' do
my_favorites = {}
expect(favorite_categories(my_favorites)).to eq([])
end
end

describe 'favorite items exercise' do

xit 'returns an array with the string and integer values' do
it 'returns an array with the string and integer values' do
my_favorites = { color: 'blue', number: 65, movie: 'Avengers: Endgame' }
expect(favorite_items(my_favorites)).to eq(['blue', 65, 'Avengers: Endgame'])
end

xit 'returns an array with the array and hash values' do
it 'returns an array with the array and hash values' do
my_favorites = { color: ['orange', 'green'], number: { lucky: 7 } }
expect(favorite_items(my_favorites)).to eq([['orange', 'green'], { lucky: 7 }])
end

xit 'returns an empty array when hash is empty' do
it 'returns an empty array when hash is empty' do
my_favorites = {}
expect(favorite_items(my_favorites)).to eq([])
end
end

describe 'merge favorites exercise' do

xit 'returns a hash with all key/value pairs when there is not a duplicate' do
it 'returns a hash with all key/value pairs when there is not a duplicate' do
my_favorites = { color: 'blue', number: 65 }
favorite_movie = { movie: 'Avengers: Endgame' }
result = { color: 'blue', number: 65, movie: 'Avengers: Endgame' }
expect(merge_favorites(my_favorites, favorite_movie)).to eq(result)
end

xit 'returns a hash with an updated value when there is duplicate' do
it 'returns a hash with an updated value when there is duplicate' do
my_favorites = { color: 'emerald green', movie: 'Avengers: Endgame' }
favorite_movie = { movie: 'Avengers: Infinity War' }
result = { color: 'emerald green', movie: 'Avengers: Infinity War' }
expect(merge_favorites(my_favorites, favorite_movie)).to eq(result)
end

xit 'returns a hash with all key/value pairs and an updated value when there is duplicate' do
it 'returns a hash with all key/value pairs and an updated value when there is duplicate' do
my_favorites = { color: 'teal', number: 65 }
new_favorites = { number: 42, movie: "The Hitchhiker's Guide to the Galaxy" }
result = { color: 'teal', number: 42, movie: "The Hitchhiker's Guide to the Galaxy" }
Expand Down
33 changes: 32 additions & 1 deletion ruby_basics/8_methods/exercises/method_exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,48 @@
# parameter: number (an integer)
# return value: the number's ASCII character (https://www.ascii-code.com/)
# hint: use Integer#chr
def ascii_translator(number)
number.chr
end



# method name: #common_sports
# parameters: current_sports and favorite_sports (both arrays)
# return value: an array containing items in both arrays
# hint: use Array#intersection
def common_sports(current_sports, favorite_sports)
current_sports.intersection(favorite_sports)
end


# method name: #alphabetical_list
# parameter: games (an array)
# return value: games, alphabetically sorted and duplicates removed
# hint: chain Array#sort and Array#uniq together
def alphabetical_list(games)
games.sort.uniq
end


# method name: #lucky_number
# parameter: number (an integer) with default value of 7
# return value: a string "Today's lucky number is <number>"
def lucky_number(number = 7)
"Today's lucky number is #{number}"
end


# method name: #ascii_code
# parameter: character (a string)
# return value: the character's ordinal number
# explicit return value: 'Input Error' if character's length does not equal 1
# hint: use String#ord
def ascii_code(character)
return 'Input Error' unless character.length == 1

character.ord
end


# method name: #pet_pun
Expand All @@ -40,9 +58,22 @@
# console output: otherwise, "I think <animal>s have pet-tential!" (potential)
# hint: use puts

def pet_pun(animal)
case animal
when 'cat'
puts "Cats are purr-fect!"
when 'dog'
puts "Dogs are paw-some!"
else
puts "I think #{animal}s have pet-tential!"
end
end


# method name: #twenty_first_century?
# parameter: year (an integer)
# return value: true if the year is between 2001 - 2100, otherwise return false
# hint: use Comparable#between?

def twenty_first_century?(year)
year.between?(2001, 2100)
end
40 changes: 20 additions & 20 deletions ruby_basics/8_methods/spec/method_exercises_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
end

# remove the 'x' from the line below to unskip the test
xit 'returns a lowercase z' do
it 'returns a lowercase z' do
expect(ascii_translator(122)).to eq('z')
end

xit 'returns an exclamation mark' do
it 'returns an exclamation mark' do
expect(ascii_translator(33)).to eq('!')
end
end

describe 'common sports exercise using #intersection' do

xit 'returns the common sports' do
it 'returns the common sports' do
current = ['tennis', 'football', 'baseball']
favorite = ['baseball', 'tennis', 'basketball']
expect(common_sports(current, favorite)).to eq(['tennis', 'baseball'])
end

xit 'returns an empty array when there are no common sports' do
it 'returns an empty array when there are no common sports' do
current = ['tennis', 'football', 'wrestling']
favorite = ['baseball', 'basketball']
expect(common_sports(current, favorite)).to eq([])
Expand All @@ -36,82 +36,82 @@

describe 'alphabetical list exercise using #sort and #uniq chained' do

xit 'returns an sorted array removing one duplicate' do
it 'returns an sorted array removing one duplicate' do
games = ['Chess', 'Scrabble', 'Othello', 'Chess']
expect(alphabetical_list(games)).to eq(['Chess', 'Othello', 'Scrabble'])
end

xit 'returns an sorted array removing multiple duplicates' do
it 'returns an sorted array removing multiple duplicates' do
games = ['Monopoly', 'Checkers', 'Risk', 'Checkers', 'Risk', 'Checkers']
expect(alphabetical_list(games)).to eq(['Checkers', 'Monopoly', 'Risk'])
end
end

describe 'lucky number exercise using a default parameter' do

xit 'returns a string with the provided argument' do
it 'returns a string with the provided argument' do
expect(lucky_number(42)).to eq("Today's lucky number is 42")
end

xit 'returns a string with the default parameter' do
it 'returns a string with the default parameter' do
expect(lucky_number).to eq("Today's lucky number is 7")
end
end

describe 'ASCII code exercise using implicit and explicit return' do

xit 'returns number for uppercase letter' do
it 'returns number for uppercase letter' do
expect(ascii_code('A')).to eq(65)
end

xit 'returns number for lowercase letter' do
it 'returns number for lowercase letter' do
expect(ascii_code('z')).to eq(122)
end

xit 'returns input error when there is less than one character' do
it 'returns input error when there is less than one character' do
expect(ascii_code('')).to eq('Input Error')
end

xit 'returns input error when there is more than one character' do
it 'returns input error when there is more than one character' do
expect(ascii_code('word')).to eq('Input Error')
end
end

describe 'pet pun exercise using console output' do

xit 'returns nil' do
it 'returns nil' do
allow($stdout).to receive(:puts).with("Cats are purr-fect!")
expect(pet_pun('cat')).to be nil
end

xit 'outputs the cat pun' do
it 'outputs the cat pun' do
expect { pet_pun('cat') }.to output("Cats are purr-fect!\n").to_stdout
end

xit 'outputs the dog pun' do
it 'outputs the dog pun' do
expect { pet_pun('dog') }.to output("Dogs are paw-some!\n").to_stdout
end

xit 'outputs the default pet pun' do
it 'outputs the default pet pun' do
expect { pet_pun('rabbit') }.to output("I think rabbits have pet-tential!\n").to_stdout
end
end

describe 'twenty-first century predicate exercise' do

xit 'returns true when the year is between 2001 - 2100' do
it 'returns true when the year is between 2001 - 2100' do
expect(twenty_first_century?(2024)).to be true
end

xit 'returns true when the year is 2001' do
it 'returns true when the year is 2001' do
expect(twenty_first_century?(2001)).to be true
end

xit 'returns true when the year is 2100' do
it 'returns true when the year is 2100' do
expect(twenty_first_century?(2100)).to be true
end

xit 'returns false when the year is not between 2001 - 2100' do
it 'returns false when the year is not between 2001 - 2100' do
expect(twenty_first_century?(1999)).to be false
end
end
Expand Down

0 comments on commit 32c174b

Please sign in to comment.