Strings
Data Types
Regular Expressions
Declarative Programming
Advanced Language Features
Programming Paradigms
Fundamentals
class String def justify(width) return self if self.length <= width newline = "\n" paragraph_separator = newline * 2 space = ' ' paragraphs = split paragraph_separator if paragraphs.length > 1 paragraphs.map {|paragraph| paragraph.justify width }.join paragraph_separator elsif include? newline gsub(newline, space).justify width else space_index = slice(0, width.succ).rindex(space) || index(space, width) [slice(0, space_index), slice(space_index..-1).lstrip.justify(width)].join newline end end end
class Stringdef justify(len)unless self.length < lenwords = self.scan(/[\w,.-]+(?:\n{2,})?/)actual_len = 0output = ""words.each do |w|if actual_len >= lenoutput += "\n"elseoutput += " " if output > "" and !output[-1].eql?"\n" and actual_len + w.length<=lenendactual_len = 0 if output[-1].eql?"\n"if (actual_len == 0) or actual_len + w.length<=lenoutput += wactual_len += w.length + 1elseoutput += "\n" + wactual_len = w.length + 1endendreturn output.rstripelseselfendendend- class String
- def justify(width)
- return self if self.length <= width
- newline = "\n"
- paragraph_separator = newline * 2
- space = ' '
- paragraphs = split paragraph_separator
- if paragraphs.length > 1
- paragraphs.map {|paragraph| paragraph.justify width }.join paragraph_separator
- elsif include? newline
- gsub(newline, space).justify width
- else
- space_index = slice(0, width.succ).rindex(space) || index(space, width)
- [slice(0, space_index), slice(space_index..-1).lstrip.justify(width)].join newline
- end
- end
- end
describe "Justify" do it "Should format a long line of text breaking it up for a given width" do Test.assert_equals( "Roses are red, violets are blue".justify(20), "Roses are red,\nviolets are blue") Test.assert_equals( "Chapter 1\n\nRoses\nare red,\nviolets\nare blue".justify(20), "Chapter 1\n\nRoses are red,\nviolets are blue", "should keep paragraphs") Test.assert_equals( "a b c d e".justify(3), "a b\nc d\ne") Test.assert_equals( "aa bb cc dd".justify(3), "aa\nbb\ncc\ndd") Test.assert_equals( "a b c d e f".justify(5), "a b c\nd e f") Test.assert_equals( "a b c d e".justify(9), "a b c d e", 'no extra spaces added') Test.assert_equals( "a b c d e".justify(3), "a b\nc d\ne", 'no extra spaces added in multiline case') Test.assert_equals( "a b c d e".justify(3).split("\n").map{|a| a.length }.max, 3, 'keeps line length less or equal to defined') Test.assert_equals( "a basd c d e".justify(3), "a\nbasd\nc d\ne", 'extralong word goes to next line') end end
describe "Justify" doit "Should format a long line of text breaking it up for a given width" doTest.assert_equals( "Roses are red, violets are blue".justify(20), "Roses are red,violets are blue")Test.assert_equals( "Chapter 1Rosesare red,violetsare blue".justify(20),"Chapter 1Roses are red,violets are blue", "should keep paragraphs")Test.assert_equals( "a b c d e".justify(3), "a bc de")Test.assert_equals( "aa bb cc dd".justify(3), "aabbccdd")Test.assert_equals( "a b c d e f".justify(5), "a b cd e f")Test.assert_equals( "a b c d e".justify(9), "a b c d e", 'no extra spaces added')Test.assert_equals( "a b c d e".justify(3), "a bc de",'no extra spaces added in multiline case')Test.assert_equals( "a b c d e".justify(3).split("").map{|a| a.length }.max, 3,'keeps line length less or equal to defined')Test.assert_equals( "a basd c d e".justify(3), "abasdc de",'extralong word goes to next line')endend- describe "Justify" do
- it "Should format a long line of text breaking it up for a given width" do
- Test.assert_equals( "Roses are red, violets are blue".justify(20), "Roses are red,
- violets are blue")
- Test.assert_equals( "Chapter 1
- Roses
- are red,
- violets
- are blue".justify(20),
- "Chapter 1
- Roses are red,
- violets are blue", "should keep paragraphs")
- Test.assert_equals( "a b c d e".justify(3), "a b
- c d
- e")
- Test.assert_equals( "aa bb cc dd".justify(3), "aa
- bb
- cc
- dd")
- Test.assert_equals( "a b c d e f".justify(5), "a b c
- d e f")
- Test.assert_equals( "a b c d e".justify(9), "a b c d e", 'no extra spaces added')
- Test.assert_equals( "a b c d e".justify(3), "a b
- c d
- e",
- 'no extra spaces added in multiline case')
- Test.assert_equals( "a b c d e".justify(3).split("
- ").map{|a| a.length }.max, 3,
- 'keeps line length less or equal to defined')
- Test.assert_equals( "a basd c d e".justify(3), "a
- basd
- c d
- e",
- 'extralong word goes to next line')
- end
- end