lib/muflax/string.rb in muflax-0.2.6 vs lib/muflax/string.rb in muflax-0.3.0
- old
+ new
@@ -2,21 +2,13 @@
# -*- encoding: utf-8 -*-
# Copyright muflax <mail@muflax.com>, 2013
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
class String
- alias :starts_with? :start_with?
- alias :ends_with? :end_with?
+ alias :starts_with? :start_with?
+ alias :ends_with? :end_with?
- def align str=" ", alignment: :left
- self.split("\n").align(str, alignment: alignment).join("\n")
- end
-
- def align! str=" ", alignment: :left
- self.replace(self.align(str, aligment: alignment))
- end
-
def whackuum str=/\s+/
self.split(str).map(&:strip)
end
ANSIColorRegexp = Regexp.new(/(?: \e \[ \d+ (?: ;\d+)* m )+/x)
@@ -43,7 +35,31 @@
out = out[0, cut_at]
out << omission
end
out
+ end
+
+ def first limit=1
+ if limit == 0 ; ""
+ elsif limit >= size ; dup
+ else ; self[0..(limit - 1)]
+ end
+ end
+
+ def last limit=1
+ if limit == 0 ; ""
+ elsif limit >= size ; dup
+ else ; self[(-limit)..-1]
+ end
+ end
+
+ def indent! amount, indent_string = nil, indent_empty_lines = false
+ indent_string = indent_string || self[/^[ \t]/] || " "
+ re = indent_empty_lines ? /^/ : /^(?!$)/
+ gsub!(re, indent_string * amount)
+ end
+
+ def indent amount, indent_string = nil, indent_empty_lines = false
+ dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) }
end
end