facets 4.0.1 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +10 -0
- data/HISTORY.md +222 -0
- data/README.md +2 -2
- data/lib/core/facets/array/commonality.rb +1 -1
- data/lib/core/facets/array/delete_first.rb +6 -5
- data/lib/core/facets/array/divide.rb +1 -1
- data/lib/core/facets/array/from_base.rb +68 -0
- data/lib/core/facets/array/indexes.rb +1 -1
- data/lib/core/facets/array/occur.rb +1 -1
- data/lib/core/facets/array/split.rb +16 -9
- data/lib/core/facets/array/split_at.rb +3 -3
- data/lib/core/facets/array.rb +1 -0
- data/lib/core/facets/boolean.rb +2 -2
- data/lib/core/facets/class/descendants.rb +26 -12
- data/lib/core/facets/dir/ascend.rb +1 -2
- data/lib/core/facets/dir/multiglob.rb +4 -9
- data/lib/core/facets/enumerable/apply.rb +3 -3
- data/lib/core/facets/enumerable/cluster.rb +2 -2
- data/lib/core/facets/enumerable/defer.rb +4 -1
- data/lib/core/facets/enumerable/hashify.rb +5 -5
- data/lib/core/facets/enumerable/with_position.rb +2 -2
- data/lib/core/facets/enumerator/fx.rb +11 -11
- data/lib/core/facets/essentials.rb +2 -2
- data/lib/core/facets/file/atomic_open.rb +1 -1
- data/lib/core/facets/file/rewrite.rb +4 -4
- data/lib/core/facets/hash/count.rb +19 -8
- data/lib/core/facets/hash/deep_merge.rb +25 -20
- data/lib/core/facets/hash/deep_rekey.rb +1 -1
- data/lib/core/facets/hash/except.rb +1 -1
- data/lib/core/facets/hash/op_mul.rb +2 -0
- data/lib/core/facets/hash/op_push.rb +7 -7
- data/lib/core/facets/hash/rekey.rb +7 -7
- data/lib/core/facets/hash/weave.rb +1 -1
- data/lib/core/facets/integer/ordinal.rb +18 -13
- data/lib/core/facets/integer/to_base.rb +115 -0
- data/lib/core/facets/integer.rb +1 -0
- data/lib/core/facets/kernel/ask.rb +3 -3
- data/lib/core/facets/kernel/blank.rb +13 -8
- data/lib/core/facets/kernel/callstack.rb +2 -2
- data/lib/core/facets/kernel/d.rb +3 -3
- data/lib/core/facets/kernel/demo.rb +2 -2
- data/lib/core/facets/kernel/extend.rb +20 -4
- data/lib/core/facets/kernel/functor.rb +1 -1
- data/lib/core/facets/kernel/instance_assign.rb +2 -3
- data/lib/core/facets/kernel/instance_replace.rb +2 -2
- data/lib/core/facets/kernel/maybe.rb +1 -1
- data/lib/core/facets/kernel/meta_class.rb +2 -1
- data/lib/core/facets/kernel/meta_def.rb +3 -3
- data/lib/core/facets/kernel/object_hexid.rb +2 -7
- data/lib/core/facets/kernel/try.rb +3 -1
- data/lib/core/facets/module/attr_class_accessor.rb +2 -2
- data/lib/core/facets/module/class_accessor.rb +2 -2
- data/lib/core/facets/module/instance_method.rb +2 -2
- data/lib/core/facets/module/is.rb +1 -1
- data/lib/core/facets/numeric/delimit.rb +1 -1
- data/lib/core/facets/proc/wrap.rb +1 -1
- data/lib/core/facets/range/op_sub.rb +2 -2
- data/lib/core/facets/range/overlap.rb +32 -1
- data/lib/core/facets/range/within.rb +1 -1
- data/lib/core/facets/regexp/op_or.rb +1 -1
- data/lib/core/facets/string/align.rb +4 -4
- data/lib/core/facets/string/ascii_only.rb +2 -2
- data/lib/core/facets/string/dashcase.rb +1 -1
- data/lib/core/facets/string/file.rb +1 -1
- data/lib/core/facets/string/from_base.rb +85 -0
- data/lib/core/facets/string/indent.rb +35 -23
- data/lib/core/facets/string/margin.rb +18 -4
- data/lib/core/facets/string/mask.rb +264 -0
- data/lib/core/facets/string/natcmp.rb +1 -1
- data/lib/core/facets/string/random.rb +1 -1
- data/lib/core/facets/string/remove.rb +13 -8
- data/lib/core/facets/string/rotate.rb +1 -1
- data/lib/core/facets/string/similarity.rb +2 -2
- data/lib/core/facets/string/squish.rb +5 -3
- data/lib/core/facets/string/trim.rb +1 -1
- data/lib/core/facets/string/unfold.rb +1 -1
- data/lib/core/facets/string/word_wrap.rb +1 -1
- data/lib/core/facets/string/words.rb +2 -2
- data/lib/core/facets/string.rb +3 -0
- data/lib/core/facets/symbol/call.rb +0 -9
- data/lib/core/facets/time/change.rb +51 -29
- data/lib/core/facets/time/future.rb +6 -2
- data/lib/core/facets/time/stamp.rb +1 -1
- data/lib/core/facets/time/to_time.rb +3 -7
- data/lib/core/facets/version.rb +1 -1
- data/lib/rails/facets/file/atomic_write.rb +1 -1
- data/lib/standard/facets/crypt3.rb +4 -2
- data/lib/standard/facets/digest/salted_digest.rb +6 -2
- data/lib/standard/facets/fileutils/ln_r.rb +7 -56
- data/lib/standard/facets/mirror.rb +276 -0
- data/lib/standard/facets/ostruct/initialize.rb +7 -6
- data/lib/standard/facets/ostruct.rb +3 -3
- data/lib/standard/facets/paramix.rb +150 -0
- data/lib/standard/facets/pathname/glob.rb +11 -7
- data/lib/standard/facets/shellwords.rb +53 -5
- data/lib/standard/facets/zlib.rb +5 -9
- metadata +7 -1
|
@@ -26,8 +26,8 @@ module Kernel
|
|
|
26
26
|
# by the class of object, in the same manner as Array#replace.
|
|
27
27
|
#
|
|
28
28
|
# NOTE: Conceptually a better alternative is `instance.replace` provided
|
|
29
|
-
#
|
|
30
|
-
#
|
|
29
|
+
# by the `instance` gem. However, practically this method is probably
|
|
30
|
+
# the better choice until such time that Ruby support anonymous delegators.
|
|
31
31
|
#
|
|
32
32
|
def instance_replace(source)
|
|
33
33
|
raise ArgumentError, "not a #{self.class} -- #{source}" unless source.is_a?(self.class)
|
|
@@ -2,7 +2,8 @@ module Kernel
|
|
|
2
2
|
|
|
3
3
|
# Easy access to an object's singleton class, with optional block eval.
|
|
4
4
|
#
|
|
5
|
-
# obj.
|
|
5
|
+
# obj = Object.new
|
|
6
|
+
# obj.meta_class #=> obj.singleton_class
|
|
6
7
|
# obj.meta_class { def foo; end }
|
|
7
8
|
#
|
|
8
9
|
# This is a concise alternative to Ruby's `singleton_class` that also
|
|
@@ -17,17 +17,12 @@ module Kernel
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
# Returns the object id as a string in hexideciaml,
|
|
20
|
-
# which is how Ruby reports them with inspect
|
|
20
|
+
# which is how Ruby reports them with inspect:
|
|
21
21
|
#
|
|
22
|
-
# "ABC".object_hexid #=> "
|
|
22
|
+
# "ABC".object_hexid #=> "0x000000000006b0"
|
|
23
23
|
#
|
|
24
24
|
def object_hexid
|
|
25
25
|
HEXID_TEMPLATE % (__id__ << 1)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
# The 1.9.3 definition is as follows:
|
|
29
|
-
#def object_hexid
|
|
30
|
-
# "0x" + [object_id << 1].pack("L>!").unpack("H*")[0]
|
|
31
|
-
#end
|
|
32
|
-
|
|
33
28
|
end
|
|
@@ -8,9 +8,11 @@ module Kernel
|
|
|
8
8
|
# exactly like a normal call (or &.): a method the receiver doesn't have,
|
|
9
9
|
# or a private one, raises NoMethodError.
|
|
10
10
|
#
|
|
11
|
+
# @example = Struct.new(:name).new("bob")
|
|
12
|
+
#
|
|
11
13
|
# @example.try(:name) #=> "bob"
|
|
12
14
|
# @example.try { |o| o.name } #=> "bob"
|
|
13
|
-
# @example.try.name #=> "bob"
|
|
15
|
+
# @example.try.name #=> "bob"
|
|
14
16
|
# nil.try.name #=> nil
|
|
15
17
|
#
|
|
16
18
|
# NOTE: THIS IS NOT ACTIVESUPPORT'S #try. SINCE RAILS 4.0, ACTIVESUPPORT'S
|
|
@@ -17,7 +17,7 @@ class Module
|
|
|
17
17
|
# loaded automatically when using <code>require 'facets'</code>.
|
|
18
18
|
#
|
|
19
19
|
# @uncommon
|
|
20
|
-
# require 'facets/module/
|
|
20
|
+
# require 'facets/module/attr_class_accessor'
|
|
21
21
|
#
|
|
22
22
|
def attr_class_reader(name)
|
|
23
23
|
module_eval(<<-EOS, __FILE__, __LINE__)
|
|
@@ -35,7 +35,7 @@ class Module
|
|
|
35
35
|
# loaded automatically when using <code>require 'facets'</code>.
|
|
36
36
|
#
|
|
37
37
|
# @uncommon
|
|
38
|
-
# require 'facets/module/
|
|
38
|
+
# require 'facets/module/attr_class_accessor'
|
|
39
39
|
#
|
|
40
40
|
def attr_class_writer(name)
|
|
41
41
|
module_eval(<<-EOS, __FILE__, __LINE__)
|
|
@@ -17,7 +17,7 @@ class Module
|
|
|
17
17
|
# loaded automatically when using <code>require 'facets'</code>.
|
|
18
18
|
#
|
|
19
19
|
# @uncommon
|
|
20
|
-
# require 'facets/module/
|
|
20
|
+
# require 'facets/module/class_accessor'
|
|
21
21
|
#
|
|
22
22
|
def class_reader(name)
|
|
23
23
|
module_eval(<<-EOS, __FILE__, __LINE__)
|
|
@@ -32,7 +32,7 @@ class Module
|
|
|
32
32
|
# loaded automatically when using <code>require 'facets'</code>.
|
|
33
33
|
#
|
|
34
34
|
# @uncommon
|
|
35
|
-
# require 'facets/module/
|
|
35
|
+
# require 'facets/module/class_accessor'
|
|
36
36
|
#
|
|
37
37
|
def class_writer(name)
|
|
38
38
|
module_eval(<<-EOS, __FILE__, __LINE__)
|
|
@@ -3,7 +3,7 @@ $FIRST_CLASS_INSTANCE_METHODS = Hash.new{ |h,k| h[k] = {} }
|
|
|
3
3
|
#
|
|
4
4
|
class Module
|
|
5
5
|
|
|
6
|
-
# Access method as a singleton object and retain state
|
|
6
|
+
# Access method as a singleton object and retain state:
|
|
7
7
|
#
|
|
8
8
|
# module ::K
|
|
9
9
|
# def hello
|
|
@@ -11,7 +11,7 @@ class Module
|
|
|
11
11
|
# end
|
|
12
12
|
# end
|
|
13
13
|
#
|
|
14
|
-
# K.instance_method!(:hello).inspect #=> "#<UnboundMethod: K#hello
|
|
14
|
+
# K.instance_method!(:hello).inspect #=> "#<UnboundMethod: K#hello() ...>"
|
|
15
15
|
#
|
|
16
16
|
# NOTE: This is limited to the scope of the current module/class.
|
|
17
17
|
|
|
@@ -41,7 +41,7 @@ class Module
|
|
|
41
41
|
# Expirmental idea for #is.
|
|
42
42
|
#
|
|
43
43
|
# If the module has #append_feature_function
|
|
44
|
-
# defined, this will use that instead of #include
|
|
44
|
+
# defined, this will use that instead of #include:
|
|
45
45
|
#
|
|
46
46
|
# def is(*modules)
|
|
47
47
|
# module.each do { |m|
|
|
@@ -9,7 +9,7 @@ class Numeric
|
|
|
9
9
|
# * <tt>separator</tt> - defaults to "." but can be any string
|
|
10
10
|
#
|
|
11
11
|
# 1000.delimit(:delimiter => "_") # => "1_000"
|
|
12
|
-
# (1000.00).delimit(:delimiter => ".", :separator => ",") # => "1.000,
|
|
12
|
+
# (1000.00).delimit(:delimiter => ".", :separator => ",") # => "1.000,0"
|
|
13
13
|
def delimit(options = {})
|
|
14
14
|
opts = { :delimiter => ',', :separator => '.' }.merge(options)
|
|
15
15
|
digits, decimals = self.to_s.split('.')
|
|
@@ -7,12 +7,43 @@ class Range
|
|
|
7
7
|
#
|
|
8
8
|
# Ruby 3.3 added a native Range#overlap?, so only define Facets' version
|
|
9
9
|
# on older Rubies that lack it -- this avoids replacing the built-in.
|
|
10
|
+
# It follows Ruby's own algorithm, so it gives the same answers: an empty
|
|
11
|
+
# range overlaps nothing, beginless and endless ranges work, and a
|
|
12
|
+
# non-Range argument raises TypeError.
|
|
10
13
|
#
|
|
11
14
|
# CREDIT: Daniel Schierbeck, Brandon Keepers
|
|
12
15
|
|
|
13
16
|
unless method_defined?(:overlap?)
|
|
14
17
|
def overlap?(other)
|
|
15
|
-
|
|
18
|
+
unless other.is_a?(Range)
|
|
19
|
+
raise TypeError, "wrong argument type #{other.class} (expected Range)"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
self_beg, self_end, self_excl = self.begin, self.end, exclude_end?
|
|
23
|
+
other_beg, other_end, other_excl = other.begin, other.end, other.exclude_end?
|
|
24
|
+
|
|
25
|
+
# Is the span from b to e empty? Values that can't be compared count
|
|
26
|
+
# as empty, as in Ruby.
|
|
27
|
+
empty = lambda do |b, e, excl|
|
|
28
|
+
next false if b.nil? || e.nil?
|
|
29
|
+
cmp = b <=> e
|
|
30
|
+
cmp.nil? || cmp > 0 || (excl && cmp == 0)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
return false if empty.(self_beg, other_end, other_excl)
|
|
34
|
+
return false if empty.(other_beg, self_end, self_excl)
|
|
35
|
+
|
|
36
|
+
if !self_beg.nil? && !other_beg.nil?
|
|
37
|
+
cmp = self_beg <=> other_beg
|
|
38
|
+
return false if cmp.nil?
|
|
39
|
+
return true if cmp == 0
|
|
40
|
+
elsif self_beg.nil? && !self_end.nil? && other_beg.nil? && !other_end.nil?
|
|
41
|
+
return !(self_end <=> other_end).nil?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
return false if empty.(self_beg, self_end, self_excl)
|
|
45
|
+
return false if empty.(other_beg, other_end, other_excl)
|
|
46
|
+
true
|
|
16
47
|
end
|
|
17
48
|
end
|
|
18
49
|
|
|
@@ -11,7 +11,7 @@ class String
|
|
|
11
11
|
#
|
|
12
12
|
# s.align(:right, 14)
|
|
13
13
|
#
|
|
14
|
-
# _produces_
|
|
14
|
+
# _produces_:
|
|
15
15
|
#
|
|
16
16
|
# This is a test
|
|
17
17
|
# and
|
|
@@ -45,7 +45,7 @@ class String
|
|
|
45
45
|
#
|
|
46
46
|
# s.align_right(14)
|
|
47
47
|
#
|
|
48
|
-
# _produces_
|
|
48
|
+
# _produces_:
|
|
49
49
|
#
|
|
50
50
|
# This is a test
|
|
51
51
|
# and
|
|
@@ -75,7 +75,7 @@ class String
|
|
|
75
75
|
#
|
|
76
76
|
# s.align_left(20, "\n", '.')
|
|
77
77
|
#
|
|
78
|
-
# _produces_
|
|
78
|
+
# _produces_:
|
|
79
79
|
#
|
|
80
80
|
# This is a test......
|
|
81
81
|
# and.................
|
|
@@ -105,7 +105,7 @@ class String
|
|
|
105
105
|
#
|
|
106
106
|
# s.align_center(14)
|
|
107
107
|
#
|
|
108
|
-
# _produces_
|
|
108
|
+
# _produces_:
|
|
109
109
|
#
|
|
110
110
|
# This is a test
|
|
111
111
|
# and
|
|
@@ -2,7 +2,7 @@ class String
|
|
|
2
2
|
# Get a new string with non-ASCII characters removed.
|
|
3
3
|
#
|
|
4
4
|
# alt - String to replace non-ASCII characters with.
|
|
5
|
-
#
|
|
5
|
+
# Defaults to a blank string (`''`).
|
|
6
6
|
#
|
|
7
7
|
# Examples
|
|
8
8
|
#
|
|
@@ -27,7 +27,7 @@ class String
|
|
|
27
27
|
# Modify string keeping only ASCII characters.
|
|
28
28
|
#
|
|
29
29
|
# alt - String to replace non-ASCII characters with.
|
|
30
|
-
#
|
|
30
|
+
# Defaults to a blank string (`''`).
|
|
31
31
|
#
|
|
32
32
|
# Examples
|
|
33
33
|
#
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
class String
|
|
2
|
+
|
|
3
|
+
# Reads the string as a number in any base from 2 to 62, or with a
|
|
4
|
+
# custom set of digits, and returns it as an Integer.
|
|
5
|
+
#
|
|
6
|
+
# Ruby's own `to_i(base)` stops at base 36. Above that, the digits are
|
|
7
|
+
# 0-9, then a-z, then A-Z. Up to base 36, case doesn't matter, as with
|
|
8
|
+
# `to_i(base)`.
|
|
9
|
+
#
|
|
10
|
+
# "ff".from_base(16) #=> 255
|
|
11
|
+
# "FF".from_base(16) #=> 255
|
|
12
|
+
# "47".from_base(62) #=> 255
|
|
13
|
+
# "3HLszsQsP".from_base(62) #=> 809145531659995
|
|
14
|
+
# "-47".from_base(62) #=> -255
|
|
15
|
+
#
|
|
16
|
+
# To use other digits, give them as a string or an array of
|
|
17
|
+
# single characters, starting with the digit for zero.
|
|
18
|
+
#
|
|
19
|
+
# "LOL".from_base("OLTWFS") #=> 37
|
|
20
|
+
# "LOL".from_base(%w[O L T W F S]) #=> 37
|
|
21
|
+
#
|
|
22
|
+
# In base 256 the digits are bytes, so this reads the string's bytes,
|
|
23
|
+
# most significant first, whatever its encoding.
|
|
24
|
+
#
|
|
25
|
+
# "\x01\x00".from_base(256) #=> 256
|
|
26
|
+
#
|
|
27
|
+
# Unlike `to_i`, which stops at the first character it can't read,
|
|
28
|
+
# this raises ArgumentError for a character that isn't a digit of the
|
|
29
|
+
# base, and for an empty string. A leading "-" makes the number
|
|
30
|
+
# negative, unless "-" is one of the digits, as it is in base 256.
|
|
31
|
+
#
|
|
32
|
+
# Integer#to_base converts back. Given an array of digits, it gives
|
|
33
|
+
# an array, which Array#from_base reads.
|
|
34
|
+
#
|
|
35
|
+
# Since: 4.2.0
|
|
36
|
+
|
|
37
|
+
def from_base(base)
|
|
38
|
+
case base
|
|
39
|
+
when ::Integer
|
|
40
|
+
if base == 256
|
|
41
|
+
raise ArgumentError, "no digits in #{inspect}" if empty?
|
|
42
|
+
return unpack1('H*').to_i(16)
|
|
43
|
+
end
|
|
44
|
+
unless base >= 2 && base <= 62
|
|
45
|
+
raise ArgumentError, "invalid radix #{base}"
|
|
46
|
+
end
|
|
47
|
+
digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[0, base].chars
|
|
48
|
+
ignore_case = base <= 36
|
|
49
|
+
when ::String, ::Array
|
|
50
|
+
digits = base.is_a?(::Array) ? base.map(&:to_s) : base.chars
|
|
51
|
+
unless digits.all? { |d| d.length == 1 }
|
|
52
|
+
raise ArgumentError, "digits must be single characters"
|
|
53
|
+
end
|
|
54
|
+
if digits.size < 2
|
|
55
|
+
raise ArgumentError, "a base needs at least two digits"
|
|
56
|
+
end
|
|
57
|
+
if digits.uniq.size < digits.size
|
|
58
|
+
raise ArgumentError, "digits must not repeat"
|
|
59
|
+
end
|
|
60
|
+
ignore_case = false
|
|
61
|
+
else
|
|
62
|
+
raise TypeError, "base must be an Integer, String or Array, not #{base.class}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
values = {}
|
|
66
|
+
digits.each_with_index do |d, i|
|
|
67
|
+
values[d] = i
|
|
68
|
+
values[d.upcase] = i if ignore_case
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
str = self
|
|
72
|
+
negative = str.start_with?('-') && !values.key?('-')
|
|
73
|
+
str = str[1..-1] if negative
|
|
74
|
+
raise ArgumentError, "no digits in #{inspect}" if str.empty?
|
|
75
|
+
|
|
76
|
+
n = 0
|
|
77
|
+
str.each_char do |c|
|
|
78
|
+
v = values[c]
|
|
79
|
+
raise ArgumentError, "invalid digit #{c.inspect} in #{inspect}" unless v
|
|
80
|
+
n = n * digits.size + v
|
|
81
|
+
end
|
|
82
|
+
negative ? -n : n
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
end
|
|
@@ -1,9 +1,28 @@
|
|
|
1
1
|
class String
|
|
2
2
|
|
|
3
|
-
# Indent
|
|
3
|
+
# Indent right by `n` copies of `c`, or left by removing up to `n` copies
|
|
4
|
+
# of `c` from the start of each line if `n` is negative.
|
|
4
5
|
#
|
|
5
|
-
# n
|
|
6
|
-
#
|
|
6
|
+
# "a\nb".indent(2) #=> " a\n b"
|
|
7
|
+
# " a".indent(-2) #=> " a"
|
|
8
|
+
# "a".indent(2, "-") #=> "--a"
|
|
9
|
+
#
|
|
10
|
+
# Empty lines aren't indented, unless `empty_lines` is true.
|
|
11
|
+
#
|
|
12
|
+
# "a\n\nb".indent(2) #=> " a\n\n b"
|
|
13
|
+
# "a\n\nb".indent(2, ' ', true) #=> " a\n \n b"
|
|
14
|
+
#
|
|
15
|
+
# Without `c`, it indents with the string's own indentation character,
|
|
16
|
+
# a tab or a space, or a space if the string has no indentation.
|
|
17
|
+
#
|
|
18
|
+
# "\ta".indent(1) #=> "\t\ta"
|
|
19
|
+
#
|
|
20
|
+
# This matches ActiveSupport's String#indent, so the two agree whichever
|
|
21
|
+
# is loaded. A negative `n` is a Facets addition.
|
|
22
|
+
#
|
|
23
|
+
# n - The number of copies of `c` to add or remove. [Integer]
|
|
24
|
+
# c - The indentation string. [String]
|
|
25
|
+
# empty_lines - Whether to indent empty lines too. [Boolean]
|
|
7
26
|
#
|
|
8
27
|
# Returns a new string with the indention. [String]
|
|
9
28
|
#
|
|
@@ -11,23 +30,21 @@ class String
|
|
|
11
30
|
# Credit: Trans
|
|
12
31
|
# Credit: Tyler Rick
|
|
13
32
|
|
|
14
|
-
def indent(n, c=
|
|
33
|
+
def indent(n, c=nil, empty_lines=false)
|
|
34
|
+
c ||= self[/^[ \t]/] || ' '
|
|
15
35
|
if n >= 0
|
|
16
|
-
gsub(
|
|
36
|
+
gsub(empty_lines ? /^/ : /^(?!$)/, c * n)
|
|
17
37
|
else
|
|
18
|
-
gsub(
|
|
38
|
+
gsub(/^(?:#{Regexp.escape(c)}){0,#{-n}}/, "")
|
|
19
39
|
end
|
|
20
40
|
end
|
|
21
41
|
|
|
22
42
|
# Equivalent to String#indent, but modifies the receiver in place.
|
|
23
43
|
#
|
|
24
|
-
# n - The number of spaces to indent. [Integer]
|
|
25
|
-
# c - Character to use, if other then space. [String]
|
|
26
|
-
#
|
|
27
44
|
# Returns this string with the indention. [String]
|
|
28
45
|
|
|
29
|
-
def indent!(n, c=
|
|
30
|
-
replace(indent(n,c))
|
|
46
|
+
def indent!(n, c=nil, empty_lines=false)
|
|
47
|
+
replace(indent(n, c, empty_lines))
|
|
31
48
|
end
|
|
32
49
|
|
|
33
50
|
# Remove excessive indentation. Useful for multi-line strings
|
|
@@ -37,11 +54,11 @@ class String
|
|
|
37
54
|
#
|
|
38
55
|
# Examples
|
|
39
56
|
#
|
|
40
|
-
# <<-END.unindent
|
|
57
|
+
# text = <<-END.unindent
|
|
41
58
|
# ohaie
|
|
42
59
|
# wurld
|
|
43
|
-
# END
|
|
44
|
-
# #=> "ohaie\n wurld"
|
|
60
|
+
# END
|
|
61
|
+
# text #=> "ohaie\n wurld\n"
|
|
45
62
|
#
|
|
46
63
|
# Returns a new unindented string. [String]
|
|
47
64
|
#
|
|
@@ -52,15 +69,10 @@ class String
|
|
|
52
69
|
if size
|
|
53
70
|
indent(-size)
|
|
54
71
|
else
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
char = m[0...-1]
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
size -= 1
|
|
63
|
-
indent(-size, char)
|
|
72
|
+
# The indentation of the least indented line that isn't blank.
|
|
73
|
+
prefix = scan(/^[ \t]*(?=\S)/).min_by(&:size)
|
|
74
|
+
return dup if prefix.nil? || prefix.empty?
|
|
75
|
+
gsub(/^#{Regexp.escape(prefix)}/, '')
|
|
64
76
|
end
|
|
65
77
|
end
|
|
66
78
|
|
|
@@ -14,15 +14,27 @@ class String
|
|
|
14
14
|
# by the first non-blank line, instead of the minimum white-space for all
|
|
15
15
|
# lines.
|
|
16
16
|
#
|
|
17
|
-
#
|
|
17
|
+
# " abc\n xyz".margin(0) #=> "abc\n xyz"
|
|
18
|
+
# " abc\n xyz".margin(2, lead: true) #=> " abc\nxyz"
|
|
19
|
+
#
|
|
20
|
+
# If the `:flush` option is set, relative tabbing is not preserved: every
|
|
21
|
+
# line that isn't blank gets exactly the given number of spaces before
|
|
22
|
+
# non-space, whatever spaces or tabs it started with. Blank lines are
|
|
23
|
+
# left as they are.
|
|
24
|
+
#
|
|
25
|
+
# "abc\n xyz".margin(2) #=> " abc\n xyz"
|
|
26
|
+
# "abc\n xyz".margin(2, flush: true) #=> " abc\n xyz"
|
|
27
|
+
#
|
|
28
|
+
# Arguments:
|
|
18
29
|
#
|
|
19
30
|
# num - The size of the desired margin. [Integer]
|
|
20
31
|
# opts - Margin options. [Hash]
|
|
21
32
|
#
|
|
22
|
-
# Options
|
|
33
|
+
# Options:
|
|
23
34
|
#
|
|
24
|
-
# :tab
|
|
25
|
-
# :lead
|
|
35
|
+
# :tab - Size of tab character in spaces. [Integer]
|
|
36
|
+
# :lead - Use first non-blank line as relative marker. [Boolean]
|
|
37
|
+
# :flush - Give every line the same margin. [Boolean]
|
|
26
38
|
#
|
|
27
39
|
# Returns a new String with adjusted margin. [String]
|
|
28
40
|
#
|
|
@@ -33,6 +45,8 @@ class String
|
|
|
33
45
|
# TODO: temporary to help people transition
|
|
34
46
|
raise ArgumentError, "String#margin has been renamed to #trim." unless num
|
|
35
47
|
|
|
48
|
+
return gsub(/^[ \t]*(?=\S)/, ' ' * num) if opts[:flush]
|
|
49
|
+
|
|
36
50
|
tab = opts[:tab] || 2
|
|
37
51
|
str = gsub("\t", " " * tab) # TODO: only leading tabs ?
|
|
38
52
|
|