Skip to content
This repository
Browse code

create core_ext.rb for the few borrowed Facets methods

  • Loading branch information...
commit 5c4fe7b7db948375f52d59acc1b07d902440bc6a 1 parent d2cc4bf
7rans authored December 25, 2009
31  lib/turn/core_ext.rb
... ...
@@ -0,0 +1,31 @@
  1
+# Borrowed methods from Ruby Facets.
  2
+
  3
+class String
  4
+
  5
+  # Aligns each line n spaces.
  6
+  def tab(n)
  7
+    gsub(/^ */, ' ' * n)
  8
+  end
  9
+
  10
+  # Preserves relative tabbing.
  11
+  # The first non-empty line ends up with n spaces before nonspace.
  12
+  def tabto(n)
  13
+    if self =~ /^( *)\S/
  14
+      indent(n - $1.length)
  15
+    else
  16
+      self
  17
+    end
  18
+  end
  19
+
  20
+  # Indent left or right by n spaces.
  21
+  # (This used to be called #tab and aliased as #indent.)
  22
+  def indent(n, c=' ')
  23
+    if n >= 0
  24
+      gsub(/^/, c * n)
  25
+    else
  26
+      gsub(/^#{Regexp.escape(c)}{0,#{-n}}/, "")
  27
+    end
  28
+  end
  29
+
  30
+end
  31
+
1  lib/turn/reporter.rb
... ...
@@ -1,5 +1,6 @@
1 1
 module Turn
2 2
   require 'turn/colorize'
  3
+  require 'turn/core_ext'
3 4
 
4 5
   # = Reporter
5 6
   #
8  lib/turn/reporters/progress_reporter.rb
... ...
@@ -1,14 +1,6 @@
1 1
 require 'turn/reporter'
2 2
 require 'ansi/progressbar'
3 3
 
4  
-class String
5  
-  # Used to indent strings.
6  
-  # (method borrowed from Ruby Facets).
7  
-  def tab(n)
8  
-    gsub(/^ */, ' ' * n)
9  
-  end
10  
-end
11  
-
12 4
 module Turn
13 5
 
14 6
   #

0 notes on commit 5c4fe7b

Please sign in to comment.
Something went wrong with that request. Please try again.