Skip to content

Commit

Permalink
methods in ruby: exercies completed
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay committed Jan 10, 2024
1 parent 27e9b51 commit 0c4c21d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
39 changes: 32 additions & 7 deletions ruby_basics/8_methods/exercises/method_exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,45 @@
# parameter: number (an integer)
# return value: the number's ASCII character (https://www.ascii-code.com/)
# hint: use Integer#chr

def ascii_translator(a)
return a.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)
return 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)
return 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)
return "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)
if character.length==1
return character.ord
else
return "Input Error"
end
end

# method name: #pet_pun
# parameter: animal (a string)
Expand All @@ -39,10 +53,21 @@
# console output: if animal is 'dog', 'Dogs are paw-some!' (awesome)
# console output: otherwise, "I think <animal>s have pet-tential!" (potential)
# hint: use puts

def pet_pun(animal)
if animal=='cat'
puts 'Cats are purr-fect!'
elsif animal === 'dog'
puts 'Dogs are paw-some!'
else
puts "I think #{animal}s have pet-tential!"
end
return nil
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)
return 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 0c4c21d

Please sign in to comment.