Skip to content

Instantly share code, notes, and snippets.

@kissu
Created January 23, 2025 21:06
Show Gist options
  • Save kissu/e747b6582f78ce565399ada30be42870 to your computer and use it in GitHub Desktop.
Save kissu/e747b6582f78ce565399ada30be42870 to your computer and use it in GitHub Desktop.
Yield day with plenty of iterators + RSpec
def acronym(sentence)
return sentence.split.map { |word| word[0] }.join.upcase
end
require_relative '../acronym.rb'
describe "#acronym" do
it "returns an empty string when passed an empty string" do
actual = acronym("")
expected = ""
expect(actual).to eq(expected) # passes if `actual == expected`
end
it "returns the acronym on downcased sentences" do
actual = acronym("working from home")
expected = "WFH"
expect(actual).to eq(expected)
end
it "returns the acronym on upcased sentences" do
actual = acronym("AWAY FROM KEYBOARD")
expected = "AFK"
expect(actual).to eq(expected)
end
end
def encrypt(text)
# we need array of 26 letters from the alphabet (capitalized)
# split the unencrpyted text in letters
# iterate each letter (check its a letter)
# take letter and shift it by 3 letters backwards
# join the array
alphabet = ("A".."Z").to_a
text.upcase.split("").map do |letter|
index = alphabet.index(letter)
index.nil? ? " " : alphabet[index - 3]
end.join
end
require_relative '../encrypt.rb'
describe "#encrypt" do
it "return an empty string when passed an emptry string" do
actual = encrypt("")
expected = ""
expect(actual).to eq(expected)
end
it "returns the 3-letter backward-shifted text" do
actual = encrypt("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")
expected = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"
expect(actual).to eq(expected)
end
end
@kissu
Copy link
Author

kissu commented Jan 23, 2025

Don't forget to put the spec files in a spec directory so that it looks like this

CleanShot 2025-01-23 at 22 07 04@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment