Last active
August 29, 2015 14:22
-
-
Save ciprianna/d44c99ceaa0ff1c4592b to your computer and use it in GitHub Desktop.
Paragraph Truncator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Driver | |
require_relative "paragraph_truncator.rb" | |
puts "What paragraph would you like to truncate?" | |
paragraph = gets.chomp | |
puts "Would you like to set your own character length? ('yes' or 'no') The default value is currently set to 50 characters." | |
set_own_char_length = gets.chomp.downcase | |
if set_own_char_length == "yes" | |
puts "What character length would you like to use?" | |
character_length = gets.to_i | |
else | |
puts "Okay, we'll use the default value." | |
end | |
puts "Would you like to set your own cutoff characters? ('yes' or 'no') The default cutoff characters are currently '...'." | |
set_own_cutoff_chars = gets.chomp.downcase | |
if set_own_cutoff_chars == "yes" | |
puts "What cutoff characters would you like to use?" | |
cutoff_characters = gets.chomp | |
else | |
"Okay, we'll use the default." | |
end | |
if (set_own_cutoff_chars == "yes") && (set_own_char_length == "yes") | |
text = ParagraphTruncator.new(paragraph: paragraph, cutoff_characters: cutoff_characters, character_length: character_length) | |
puts "#{text.truncate_paragraph(text.arguments)}" | |
elsif (set_own_cutoff_chars != "yes") && (set_own_char_length == "yes") | |
text = ParagraphTruncator.new(paragraph: paragraph, character_length: character_length) | |
puts "#{text.truncate_paragraph(text.arguments)}" | |
elsif (set_own_cutoff_chars == "yes") && (set_own_char_length != "yes") | |
text = ParagraphTruncator.new(paragraph: paragraph, cutoff_characters: cutoff_characters) | |
puts "#{text.truncate_paragraph(text.arguments)}" | |
else | |
text = ParagraphTruncator.new(paragraph: paragraph) | |
puts "#{text.truncate_paragraph(text.arguments)}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Paragraph Truncator | |
class ParagraphTruncator | |
attr_accessor :arguments | |
# Creates an empty Hash and then merges it with default values for the options | |
# Hash. | |
# | |
# arguments - Hash | |
# | |
# Returns the Return from the truncate_paragraph method. | |
def initialize(arguments = {}) | |
options = {paragraph: "No paragraph entered.", character_length: 50, cutoff_characters: "..."} | |
@arguments = options.merge(arguments) | |
end | |
# Takes the inputs from the arguments Hash and truncates the paragraph if its | |
# length is greater than the character_length from the arguments Hash. | |
# | |
# arguments - Hash | |
# | |
# Returns the paragraph_output, a String, or returns the original paragraph | |
def truncate_paragraph(arguments) | |
paragraph = arguments[:paragraph] | |
if paragraph.length > arguments[:character_length] | |
shortened_paragraph = paragraph[0..arguments[:character_length]] | |
paragraph_output = shortened_paragraph + arguments[:cutoff_characters] | |
return paragraph_output | |
end | |
return paragraph | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Paragraph Truncator Test | |
require "minitest/autorun" | |
require_relative "paragraph_truncator.rb" | |
class ParagraphTruncatorTest < Minitest::Test | |
# Need to test if the truncate_paragraph test returns a shortened character | |
# length with the correct cutoff characters than the original paragraph. | |
def test_truncate_paragraph | |
text_to_shorten = ParagraphTruncator.new(paragraph: "Lengthy text that goes on and on and on and on and on and on and on and on and on! Cats!") | |
output = text_to_shorten.truncate_paragraph(text_to_shorten.arguments) | |
assert(text_to_shorten.arguments[:character_length] + text_to_shorten.arguments[:cutoff_characters].length <= text_to_shorten.arguments[:paragraph].length) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment