Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active August 30, 2022 06:20
Embed
What would you like to do?
Ruby implementation of five string operations
module StringOps
module_function
def clean_up(summary:)
split_into_sentences(fix_hyphenation(fix_whitespace(summary))).first
end
def fix_whitespace(a_string)
a_string.gsub "\n", " "
end
def fix_hyphenation(a_string)
a_string.gsub "- ", ""
end
def split_into_sentences(a_string)
a_string
.split(". ")
.map { |sentence| ensure_ends_with_period(sentence) }
end
def ensure_ends_with_period(sentence)
sentence + (sentence.end_with?(".") ? "" : ".")
end
end
require 'string_ops'
include StringOps
describe StringOps do
describe '#fix_whitespace' do
it 'changes newline to a blank' do
expect( fix_whitespace "new\nline" ).to eq 'new line'
end
end
describe '#fix_hyphenation' do
it 'de-hyphenates' do
expect( fix_hyphenation 'zip- per' ).to eq 'zipper'
end
end
describe '#clean_up' do
it "handles extra text" do
input = "Relating to the state transient lodging tax; creating\nnew provisions; amending ORS 284.131 and\n320.305; prescribing an effective date; and pro-\nviding for revenue raising that requires approval\nby a three-fifths majority.\nWhereas Enrolled House Bill 2267 (chapter 818,"
expect( clean_up summary: input ).to eq "Relating to the state transient lodging tax; creating new provisions; amending ORS 284.131 and 320.305; prescribing an effective date; and providing for revenue raising that requires approval by a three-fifths majority."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment