Skip to content

Instantly share code, notes, and snippets.

@georgekettle
Created January 15, 2021 01:05
Show Gist options
  • Save georgekettle/6d40e3b6d4db372c79df5d8b8241a034 to your computer and use it in GitHub Desktop.
Save georgekettle/6d40e3b6d4db372c79df5d8b8241a034 to your computer and use it in GitHub Desktop.
def acronymize(text)
# 'text' is a string
# we need access to each word (so lets break it up) => array
words = text.split # we have an array now 👍
# get the first letter of each --> join them --> capitalize the whole word
words.map{|word| word[0]}.join.upcase
end
# 'situation normal all fucked up' => snafu
p acronymize('situation normal all fucked up')
# spec/acronymize_spec.rb
# the naming convention if this file is important, as we know it relates to our acronymize.rb file
# To use our encrypt method, let's import the file using 'require_relative'...
# (this allows us to specify our path relative to this current file)
require_relative "../acronymize"
describe "#acronymize" do
it "returns an empty string when passed an empty string" do
actual = acronymize("")
expected = ""
expect(actual).to eq(expected) # passes if `actual == expected`
end
it "returns the acronym on downcased sentences" do
actual = acronymize("working from home")
expected = "WFH"
expect(actual).to eq(expected)
end
it "returns the acronym on upcased sentences" do
actual = acronymize("AWAY FROM KEYBOARD")
expected = "AFK"
expect(actual).to eq(expected)
end
end
# define our encrypt method
def encrypt(text)
# 26 chars in the alphabet
alphabet = ("A".."Z").to_a
# get access to ech character of text
array_of_chars = text.chars
# loop through each character of our text (so that we can change it's value)
array_of_chars.map do |char|
index = alphabet.index(char)
index ? alphabet[index - 3] : char
end.join # and join all chars into single string
end
# quick test of the method
p encrypt("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")
# expected output => "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"
# spec/encrypt_spec.rb
# the naming convention if this file is important, as we know it relates to our encrypt.rb file
# To use our encrypt method, let's import the file using 'require_relative'...
# (this allows us to specify our path relative to this current file)
require_relative "../encrypt"
# give our tests a name (almost like a section of tests)
describe "#encrypt" do
# Let's start with the most basic test that we can pass
it "returns an empty string when passed an empty string" do
actual = encrypt("")
expected = ""
expect(actual).to eq(expected)
end
# Let's test to see if we get the desired result
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment