dedent 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -40,6 +40,13 @@ for f in $(ls /foo/bar/); do
40
40
  done
41
41
  ```
42
42
 
43
+ One thing to note is that lines with no non-whitespaces characters are not considered when determining the
44
+ indentation of a text block:
45
+
46
+ ```
47
+ "\n foo".dedent # => "\nfoo"
48
+ ```
49
+
43
50
  Author
44
51
  ------
45
52
 
@@ -4,8 +4,11 @@ class String
4
4
  def dedent
5
5
  lines = split "\n"
6
6
  return self if lines.empty?
7
- min_indent = lines.map { |line| line.start_with?(" ") ? line.match(/^ +/).offset(0)[1] : 0 }.min
7
+ indents = lines.map do |line|
8
+ line =~ /\S/ ? (line.start_with?(" ") ? line.match(/^ +/).offset(0)[1] : 0) : nil
9
+ end
10
+ min_indent = indents.compact.min
8
11
  return self if min_indent.zero?
9
- lines.map { |line| line.gsub(/^ {#{min_indent}}/, "") }.join("\n")
12
+ lines.map { |line| line =~ /\S/ ? line.gsub(/^ {#{min_indent}}/, "") : line }.join "\n"
10
13
  end
11
14
  end
@@ -1,3 +1,3 @@
1
1
  module Dedent
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dedent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-10 00:00:00.000000000Z
12
+ date: 2011-12-08 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: ! 'This gem adds a dedent method to strings to strip leading spaces from
15
15
  each line while preserving indentation.