Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created August 4, 2008 04:26
Show Gist options
  • Save ELLIOTTCABLE/3855 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/3855 to your computer and use it in GitHub Desktop.
# [See the specs at the bottom for a more verbose description of these requirements]
# A) Indent every line except the first (assume something as wide as INDENT
# has already been printed) to INDENT spaces in.
# B) Visible characters (non-whitespace) must never exceed WIDTH
# 1) Wrap automatically at word boundary, if there is a boundary within
# MIN_WIDTH
# 2) Wrap automatically at WIDTH, if there is no boundary within MIN_WIDTH
# C) Preserve existing whitespace (but do not include whitespace at EOL for B)
require 'rubygems'
require 'spec'
class String
def indent spaces
if spaces.respond_to? :to_s # duck,
self.split("\n").map {|s| [spaces, s].join }.join("\n")
elsif spaces.respond_to? :to_i # duck,
self.split("\n").map {|s| [(' ' * spaces), s].join }.join("\n")
else # goose!
raise ArgumentError, "#{spaces} is neither string-ish nor numeric-ish"
end
end
def wrap width
raise ArgumentError, "#{width} is not numeric-ish" unless width.respond_to? :to_i
# .{1,#{width}} # Any ammount of anything less than +width+ characters...
# \b # ... that ends at a word boundary,
# [^\w]? # ... including any punctuation, even over +width+ chars,
# \s* # ... don't worry about whitespace after +width+
self.scan(/.{1,#{width}}(\b[^\w]?)\s*/).join("\n")
end
end
describe String do
describe '#indent' do
it "should indent a single-line string" do
string = 'abcdef'
indented = string.indent(' ')
indented.should == ' abcdef'
end
it "should indent a multi-line string" do
string = "abcdef\nghijkl"
indented = string.indent(' ')
indented.should == " abcdef\n ghijkl"
end
it "should preserve whitespace" do
string = "begin\n puts 'whee!'\nend"
indented = string.indent(' ')
indented.should == " begin\n puts 'whee!'\n end"
end
end
describe '#wrap' do
it "should wrap a long, continuous string at `width`" do
string = '0123456789' * 3
wrapped = string.wrap(10)
wrapped.should == "0123456789\n0123456789\n0123456789"
end
it "should wrap a sentence at the last word boundary before `width`" do
string = 'The quick blue merle Tucker jumped over the mean black and white Jazz.'
wrapped = string.wrap(30)
wrapped.should == "The quick blue merle Tucker \njumped over the mean black and \nwhite Jazz."
end
end
end
# Text::Format doesn't work, see EOF.
require 'rubygems'
require 'text/format'
Formatter = Text::Format.new
Test = "\
0123456789ABCDEF 0123456789ABCDEF 0123456789ABCDEF 0123456789ABCDEF 0123456789ABCDEF 0123456789ABCDEF
abcdefghi
jklmno
pqr
stuv
wx
yz!
END OF LINE LAWL"
p Formatter.format(Test) #=> "\
# 0123456789ABCDEF 0123456789ABCDEF 0123456789ABCDEF 0123456789ABCDEF
# 0123456789ABCDEF 0123456789ABCDEF abcdefghi jklmno pqr stuv wx yz! END
# OF LINE LAWL
# "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment