Skip to content

Instantly share code, notes, and snippets.

@KBeltz
Last active August 29, 2015 14:22
Show Gist options
  • Save KBeltz/e2fe82cb75d4920b30d7 to your computer and use it in GitHub Desktop.
Save KBeltz/e2fe82cb75d4920b30d7 to your computer and use it in GitHub Desktop.
Paragraph Truncator
require_relative "paragraph_truncator.rb"
puts "<inspirational paragraph prompt here>:"
words = gets.chomp
puts "How many characters should be in that lovely paragraph of yours?"
number = gets.to_i
puts "What would you like to see at the end of your truncated paragraph?"
puts "(Ex: Read More, See More, ..., etc.)"
end_of_paragraph = gets.chomp
paragraph = ParagraphTruncator.new(words, number, end_of_paragraph)
puts paragraph.truncator(words, number, end_of_paragraph)
# build a paragraph truncator class
class ParagraphTruncator
attr_accessor :words, :number_of_characters, :last_characters
# initializes instance of ParagraphTruncator class
#
# words - string containing some user-provided paragraph
# number_of_characters - integer number of maximum characters
# last_characters - string inserted at the end of truncated paragraph
#
# returns self
def initialize(words, number_of_characters, last_characters)
@words = words
@number_of_characters = number_of_characters
@last_characters = last_characters
end
def truncator(words, characters, last_characters)
if words.length > number_of_characters
(words.length - number_of_characters).times do
words = words.chop
end
end
words.insert(-1, last_characters)
end
end
require "minitest/autorun"
require_relative "paragraph_truncator.rb"
class ParagraphTruncatorTest < Minitest::Test
# One of my specs is that the truncator method should reduce the number
# of characters in the paragraph to a number specified by the user and
# insert a user-defined set of characters at the end of the paragraph
def test_truncator
short_paragraph = ParagraphTruncator.new("Hey there!", 3, "...")
assert_equal("Hey...", short_paragraph.truncator("Hey there!", 3, "..."))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment