xqsr3 0.38.1.1 → 0.39.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -6
- data/examples/count_word_frequencies.md +13 -13
- data/examples/count_word_frequencies.rb +5 -5
- data/lib/xqsr3/array_utilities/join_with_or.rb +53 -54
- data/lib/xqsr3/array_utilities.rb +2 -2
- data/lib/xqsr3/command_line_utilities/map_option_string.rb +66 -67
- data/lib/xqsr3/command_line_utilities.rb +2 -2
- data/lib/xqsr3/containers/frequency_map.rb +405 -404
- data/lib/xqsr3/containers/multi_map.rb +454 -453
- data/lib/xqsr3/containers.rb +3 -3
- data/lib/xqsr3/conversion/bool_parser.rb +56 -57
- data/lib/xqsr3/conversion/integer_parser.rb +92 -93
- data/lib/xqsr3/conversion.rb +3 -3
- data/lib/xqsr3/diagnostics/exception_utilities.rb +152 -151
- data/lib/xqsr3/diagnostics/exceptions/with_cause.rb +99 -95
- data/lib/xqsr3/diagnostics/inspect_builder.rb +86 -86
- data/lib/xqsr3/diagnostics.rb +3 -3
- data/lib/xqsr3/doc_.rb +136 -136
- data/lib/xqsr3/extensions/array/join_with_or.rb +9 -9
- data/lib/xqsr3/extensions/enumerable/collect_with_index.rb +18 -17
- data/lib/xqsr3/extensions/enumerable/detect_map.rb +41 -41
- data/lib/xqsr3/extensions/enumerable/unique.rb +32 -31
- data/lib/xqsr3/extensions/hash/deep_transform.rb +1 -1
- data/lib/xqsr3/extensions/hash/except.rb +16 -16
- data/lib/xqsr3/extensions/hash/has_match.rb +10 -10
- data/lib/xqsr3/extensions/hash/match.rb +10 -10
- data/lib/xqsr3/extensions/hash/slice.rb +11 -11
- data/lib/xqsr3/extensions/hash.rb +1 -1
- data/lib/xqsr3/extensions/integer/to_s_grp.rb +118 -0
- data/lib/xqsr3/extensions/integer.rb +3 -0
- data/lib/xqsr3/extensions/io/writelines.rb +34 -34
- data/lib/xqsr3/extensions/kernel/integer.rb +26 -26
- data/lib/xqsr3/extensions/kernel/raise_with_options.rb +15 -14
- data/lib/xqsr3/extensions/kernel.rb +1 -0
- data/lib/xqsr3/extensions/string/ends_with.rb +1 -1
- data/lib/xqsr3/extensions/string/map_option_string.rb +4 -5
- data/lib/xqsr3/extensions/string/nil_if_empty.rb +1 -1
- data/lib/xqsr3/extensions/string/nil_if_whitespace.rb +1 -1
- data/lib/xqsr3/extensions/string/quote_if.rb +1 -2
- data/lib/xqsr3/extensions/string/starts_with.rb +1 -1
- data/lib/xqsr3/extensions/string/to_bool.rb +8 -9
- data/lib/xqsr3/extensions/string/to_symbol.rb +1 -1
- data/lib/xqsr3/extensions/string/truncate.rb +1 -2
- data/lib/xqsr3/extensions/test/unit/assert_eql.rb +7 -7
- data/lib/xqsr3/extensions/test/unit/assert_false.rb +6 -7
- data/lib/xqsr3/extensions/test/unit/assert_not.rb +6 -7
- data/lib/xqsr3/extensions/test/unit/assert_not_eql.rb +7 -7
- data/lib/xqsr3/extensions/test/unit/assert_raise_with_message.rb +61 -62
- data/lib/xqsr3/extensions/test/unit/assert_subclass_of.rb +7 -8
- data/lib/xqsr3/extensions/test/unit/assert_superclass_of.rb +7 -8
- data/lib/xqsr3/extensions/test/unit/assert_true.rb +6 -7
- data/lib/xqsr3/extensions/test/unit/assert_type_has_instance_methods.rb +36 -38
- data/lib/xqsr3/extensions.rb +5 -5
- data/lib/xqsr3/hash_utilities/deep_transform.rb +71 -71
- data/lib/xqsr3/hash_utilities/key_matching.rb +82 -82
- data/lib/xqsr3/hash_utilities.rb +3 -3
- data/lib/xqsr3/internal_/test_unit_version_.rb +130 -88
- data/lib/xqsr3/io/writelines.rb +125 -125
- data/lib/xqsr3/quality/parameter_checking.rb +452 -453
- data/lib/xqsr3/quality.rb +2 -2
- data/lib/xqsr3/string_utilities/ends_with.rb +64 -64
- data/lib/xqsr3/string_utilities/nil_if_empty.rb +33 -34
- data/lib/xqsr3/string_utilities/nil_if_whitespace.rb +35 -35
- data/lib/xqsr3/string_utilities/quote_if.rb +55 -56
- data/lib/xqsr3/string_utilities/starts_with.rb +64 -64
- data/lib/xqsr3/string_utilities/to_symbol.rb +80 -80
- data/lib/xqsr3/string_utilities/truncate.rb +60 -61
- data/lib/xqsr3/string_utilities.rb +8 -8
- data/lib/xqsr3/version.rb +19 -20
- data/test/performance/frequency_map.rb +13 -27
- data/test/scratch/test_assert_raise_with_message.rb +12 -13
- data/test/unit/array_utilities/tc_join_with_or.rb +140 -142
- data/test/unit/command_line_utilities/tc_map_option_string.rb +18 -18
- data/test/unit/containers/tc_frequency_map.rb +607 -554
- data/test/unit/containers/tc_multi_map.rb +573 -522
- data/test/unit/conversion/tc_integer_parser.rb +73 -73
- data/test/unit/conversion/tc_to_bool.rb +26 -26
- data/test/unit/diagnostics/exceptions/tc_with_cause.rb +165 -165
- data/test/unit/diagnostics/tc_exception_utilities.rb +156 -156
- data/test/unit/extensions/enumerable/tc_collect_with_index.rb +14 -14
- data/test/unit/extensions/enumerable/tc_detect_map.rb +20 -20
- data/test/unit/extensions/enumerable/tc_unique.rb +34 -34
- data/test/unit/extensions/hash/tc_deep_transform.rb +22 -22
- data/test/unit/extensions/hash/tc_except.rb +28 -28
- data/test/unit/extensions/hash/tc_hash.rb +1 -1
- data/test/unit/extensions/hash/tc_slice.rb +14 -14
- data/test/unit/extensions/integer/tc_to_s_grp.rb +60 -0
- data/test/unit/extensions/integer/ts_all.rb +12 -0
- data/test/unit/extensions/io/tc_writelines.rb +77 -77
- data/test/unit/extensions/kernel/tc_integer.rb +75 -75
- data/test/unit/extensions/kernel/tc_raise_with_options.rb +155 -155
- data/test/unit/extensions/object/tc_inspect.rb +51 -51
- data/test/unit/extensions/string/tc_bool.tb +24 -24
- data/test/unit/extensions/string/tc_ends_with.rb +53 -53
- data/test/unit/extensions/string/tc_map_option_string.rb +15 -15
- data/test/unit/extensions/string/tc_nil_if_empty.rb +8 -8
- data/test/unit/extensions/string/tc_nil_if_whitespace.rb +8 -8
- data/test/unit/extensions/string/tc_quote_if.rb +18 -18
- data/test/unit/extensions/string/tc_starts_with.rb +53 -53
- data/test/unit/extensions/string/tc_to_symbol.rb +26 -26
- data/test/unit/extensions/string/tc_truncate.rb +18 -18
- data/test/unit/extensions/test/unit/tc_assert_raise_with_message.rb +16 -16
- data/test/unit/extensions/test/unit/tc_assert_subclass_of.rb +9 -9
- data/test/unit/extensions/test/unit/tc_assert_superclass_of.rb +9 -9
- data/test/unit/hash_utilities/tc_has_match.rb +70 -70
- data/test/unit/hash_utilities/tc_match.rb +83 -83
- data/test/unit/io/tc_writelines.rb +106 -108
- data/test/unit/quality/tc_parameter_checking.rb +390 -390
- data/test/unit/string_utilities/tc_truncate.rb +28 -28
- data/test/unit/tc_version.rb +15 -15
- metadata +6 -2
@@ -1,16 +1,17 @@
|
|
1
1
|
|
2
2
|
# ######################################################################## #
|
3
|
-
# File:
|
3
|
+
# File: lib/xqsr3/extensions/enumerable/detect_map.rb
|
4
4
|
#
|
5
|
-
# Purpose:
|
5
|
+
# Purpose: ::Enumerable#detect_map extension
|
6
6
|
#
|
7
|
-
# Created:
|
8
|
-
# Updated:
|
7
|
+
# Created: 3rd December 2017
|
8
|
+
# Updated: 29th March 2024
|
9
9
|
#
|
10
|
-
# Home:
|
10
|
+
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
12
|
-
# Author:
|
12
|
+
# Author: Matthew Wilson
|
13
13
|
#
|
14
|
+
# Copyright (c) 2019-2024, Matthew Wilson and Synesis Information Systems
|
14
15
|
# Copyright (c) 2017-2019, Matthew Wilson and Synesis Software
|
15
16
|
# All rights reserved.
|
16
17
|
#
|
@@ -25,7 +26,7 @@
|
|
25
26
|
# notice, this list of conditions and the following disclaimer in the
|
26
27
|
# documentation and/or other materials provided with the distribution.
|
27
28
|
#
|
28
|
-
# * Neither the names of the copyright
|
29
|
+
# * Neither the names of the copyright holders nor the names of its
|
29
30
|
# contributors may be used to endorse or promote products derived from
|
30
31
|
# this software without specific prior written permission.
|
31
32
|
#
|
@@ -49,48 +50,47 @@
|
|
49
50
|
|
50
51
|
module Enumerable
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
53
|
+
# The +Enumerable+#+detect+ method provides a way to detect the presence
|
54
|
+
# of a particular value in a collection. The only constraint is that you
|
55
|
+
# get back the object unchanged.
|
56
|
+
#
|
57
|
+
# The +Enumerable+#+map+ method provides a way to transform the contents of
|
58
|
+
# a collection. The only constraint is that you get back another
|
59
|
+
# collection.
|
60
|
+
#
|
61
|
+
# This extension method, +Enumerable+#+detect_map+ combines the features
|
62
|
+
# of both, in that it detects the presence of a particular value in a
|
63
|
+
# collection and transform the detected value.
|
64
|
+
#
|
65
|
+
# [ 1, 2, 3 ].detect_map { |v| -2 * v if v > 2 } # => -6
|
66
|
+
#
|
67
|
+
# { :ab => 'cd', :ef => 'gh' }.detect_map { |k, v| v.upcase if k == :ef } # => 'GH'
|
68
|
+
#
|
69
|
+
# *Note:* The block is required (for technical reasons), and must have
|
70
|
+
# arity 1 for sequences or 2 for associations
|
71
|
+
def detect_map &block
|
71
72
|
|
72
|
-
|
73
|
-
|
73
|
+
case block.arity
|
74
|
+
when 1
|
74
75
|
|
75
|
-
|
76
|
+
self.each do |v|
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
r = yield(v) and return r
|
79
|
+
end
|
80
|
+
when 2
|
80
81
|
|
81
|
-
|
82
|
+
self.each do |k, v|
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
84
|
+
r = yield(k, v) and return r
|
85
|
+
end
|
86
|
+
else
|
86
87
|
|
87
|
-
|
88
|
-
|
88
|
+
raise ArgumentError, "detect_map requires block with arity of 1 (for sequences) or 2 (for associations); block with arity #{block.arity} given to instance of #{self.class}"
|
89
|
+
end
|
89
90
|
|
90
|
-
|
91
|
-
|
91
|
+
nil
|
92
|
+
end
|
92
93
|
end # module Enumerable
|
93
94
|
|
94
95
|
# ############################## end of file ############################# #
|
95
96
|
|
96
|
-
|
@@ -1,16 +1,17 @@
|
|
1
1
|
|
2
2
|
# ######################################################################## #
|
3
|
-
# File:
|
3
|
+
# File: lib/xqsr3/extensions/enumerable/unique.rb
|
4
4
|
#
|
5
|
-
# Purpose:
|
5
|
+
# Purpose: Adds a unique() method to the Enumerable module
|
6
6
|
#
|
7
|
-
# Created:
|
8
|
-
# Updated:
|
7
|
+
# Created: 5th March 2007
|
8
|
+
# Updated: 29th March 2024
|
9
9
|
#
|
10
|
-
# Home:
|
10
|
+
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
12
|
-
# Author:
|
12
|
+
# Author: Matthew Wilson
|
13
13
|
#
|
14
|
+
# Copyright (c) 2019-2024, Matthew Wilson and Synesis Information Systems
|
14
15
|
# Copyright (c) 2007-2019, Matthew Wilson and Synesis Software
|
15
16
|
# All rights reserved.
|
16
17
|
#
|
@@ -25,7 +26,7 @@
|
|
25
26
|
# notice, this list of conditions and the following disclaimer in the
|
26
27
|
# documentation and/or other materials provided with the distribution.
|
27
28
|
#
|
28
|
-
# * Neither the names of the copyright
|
29
|
+
# * Neither the names of the copyright holders nor the names of its
|
29
30
|
# contributors may be used to endorse or promote products derived from
|
30
31
|
# this software without specific prior written permission.
|
31
32
|
#
|
@@ -52,40 +53,40 @@
|
|
52
53
|
|
53
54
|
module Enumerable
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
# Removes all duplicate elements in a sequence subject to an optional
|
57
|
+
# two-parameter block in order to return an array containing unique
|
58
|
+
# elements
|
59
|
+
#
|
60
|
+
# [ 1, 2, 3 ].unique # => [ 1, 2, 3 ]
|
61
|
+
# [ 1, 2, 1, 3 ].unique # => [ 1, 2, 3 ]
|
62
|
+
def unique(&block)
|
62
63
|
|
63
|
-
|
64
|
+
if not block
|
64
65
|
|
65
|
-
|
66
|
-
|
66
|
+
return unique { |a, b| a == b }
|
67
|
+
else
|
67
68
|
|
68
|
-
|
69
|
+
raise ArgumentError, "block requires two parameters" unless block.arity == 2
|
69
70
|
|
70
|
-
|
71
|
+
ar = self.to_a
|
71
72
|
|
72
|
-
|
73
|
+
return ar if ar.length < 2
|
73
74
|
|
74
|
-
|
75
|
-
|
75
|
+
r = []
|
76
|
+
h = {}
|
76
77
|
|
77
|
-
|
78
|
+
ar.each do |v|
|
78
79
|
|
79
|
-
|
80
|
+
unless h.has_key?(v)
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
82
|
+
r << v
|
83
|
+
h[v] = nil
|
84
|
+
end
|
85
|
+
end
|
85
86
|
|
86
|
-
|
87
|
-
|
88
|
-
|
87
|
+
return r
|
88
|
+
end
|
89
|
+
end
|
89
90
|
end # module Enumerable
|
90
91
|
|
91
92
|
# ############################## end of file ############################# #
|
@@ -1,32 +1,32 @@
|
|
1
1
|
|
2
2
|
unless Hash.instance_methods.include? :except!
|
3
3
|
|
4
|
-
|
4
|
+
class Hash
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
# Removes from the instance any key-value pairs matching the
|
7
|
+
# keys specified in +keys_to_delete+
|
8
|
+
def except!(*keys_to_delete)
|
9
9
|
|
10
|
-
|
10
|
+
keys_to_delete.each do |key|
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
self.delete key
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
self
|
16
|
+
end
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
unless Hash.instance_methods.include? :except
|
21
21
|
|
22
|
-
|
22
|
+
class Hash
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
# Obtains a copy of the instance wherein any key-value pairs
|
25
|
+
# matching the keys specified in +keys_to_delete+ are removed
|
26
|
+
def except(*keys_to_delete)
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
self.dup.except!(*keys_to_delete)
|
29
|
+
end
|
30
|
+
end
|
31
31
|
end
|
32
32
|
|
@@ -3,16 +3,16 @@ require 'xqsr3/hash_utilities/key_matching'
|
|
3
3
|
|
4
4
|
class Hash
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
# Extended method implemented by Xqsr3::HashUtilities::KeyMatching
|
7
|
+
#
|
8
|
+
# === Signature
|
9
|
+
#
|
10
|
+
# * *Parameters:*
|
11
|
+
# - +re+ (Regexp) The regular expression
|
12
|
+
# - +options+ (Hash) See Xqsr3::HashUtilities::KeyMatching
|
13
|
+
def has_match? re, **options
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
return ::Xqsr3::HashUtilities::KeyMatching.has_match? self, re, **options
|
16
|
+
end
|
17
17
|
end
|
18
18
|
|
@@ -3,16 +3,16 @@ require 'xqsr3/hash_utilities/key_matching'
|
|
3
3
|
|
4
4
|
class Hash
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
# Extended method implemented by Xqsr3::HashUtilities::KeyMatching
|
7
|
+
#
|
8
|
+
# === Signature
|
9
|
+
#
|
10
|
+
# * *Parameters:*
|
11
|
+
# - +re+ (Regexp) The regular expression
|
12
|
+
# - +options+ (Hash) See Xqsr3::HashUtilities::KeyMatching
|
13
|
+
def match re, **options
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
return ::Xqsr3::HashUtilities::KeyMatching.match self, re, **options
|
16
|
+
end
|
17
17
|
end
|
18
18
|
|
@@ -1,22 +1,22 @@
|
|
1
1
|
|
2
2
|
unless Hash.instance_methods.include? :slice
|
3
3
|
|
4
|
-
|
4
|
+
class Hash
|
5
5
|
|
6
|
-
|
6
|
+
def slice(*args)
|
7
7
|
|
8
|
-
|
8
|
+
r = {}
|
9
9
|
|
10
|
-
|
10
|
+
args.each do |arg|
|
11
11
|
|
12
|
-
|
12
|
+
if self.has_key? arg
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
r[arg] = self[arg]
|
15
|
+
end
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
r
|
19
|
+
end
|
20
|
+
end
|
21
21
|
end
|
22
22
|
|
@@ -0,0 +1,118 @@
|
|
1
|
+
|
2
|
+
# ######################################################################## #
|
3
|
+
# File: lib/xqsr3/extensions/integer/to_s_grp.rb
|
4
|
+
#
|
5
|
+
# Purpose: Adds a to_s_grp() method to the Integer class
|
6
|
+
#
|
7
|
+
# Created: 29th March 2024
|
8
|
+
# Updated: 29th March 2024
|
9
|
+
#
|
10
|
+
# Home: http://github.com/synesissoftware/xqsr3
|
11
|
+
#
|
12
|
+
# Author: Matthew Wilson
|
13
|
+
#
|
14
|
+
# Copyright (c) 2024, Matthew Wilson and Synesis Information Systems
|
15
|
+
# All rights reserved.
|
16
|
+
#
|
17
|
+
# Redistribution and use in source and binary forms, with or without
|
18
|
+
# modification, are permitted provided that the following conditions are
|
19
|
+
# met:
|
20
|
+
#
|
21
|
+
# * Redistributions of source code must retain the above copyright
|
22
|
+
# notice, this list of conditions and the following disclaimer.
|
23
|
+
#
|
24
|
+
# * Redistributions in binary form must reproduce the above copyright
|
25
|
+
# notice, this list of conditions and the following disclaimer in the
|
26
|
+
# documentation and/or other materials provided with the distribution.
|
27
|
+
#
|
28
|
+
# * Neither the names of the copyright holder nor the names of its
|
29
|
+
# contributors may be used to endorse or promote products derived from
|
30
|
+
# this software without specific prior written permission.
|
31
|
+
#
|
32
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
33
|
+
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
34
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
35
|
+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
36
|
+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
37
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
38
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
39
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
40
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
41
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
42
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
43
|
+
#
|
44
|
+
# ######################################################################## #
|
45
|
+
|
46
|
+
|
47
|
+
# ##########################################################
|
48
|
+
# ::Integer
|
49
|
+
|
50
|
+
=begin
|
51
|
+
=end
|
52
|
+
|
53
|
+
class Integer
|
54
|
+
|
55
|
+
# Extends +Integer+ type with the +#to_s_grp()+ method
|
56
|
+
def to_s_grp *args, **options
|
57
|
+
|
58
|
+
separator = options[:separator] || ','
|
59
|
+
|
60
|
+
numbers =
|
61
|
+
case args.size
|
62
|
+
when 0
|
63
|
+
|
64
|
+
[]
|
65
|
+
when 1
|
66
|
+
case args[0]
|
67
|
+
when ::Array
|
68
|
+
|
69
|
+
args[0]
|
70
|
+
when ::Integer
|
71
|
+
|
72
|
+
[ args[0] ]
|
73
|
+
else
|
74
|
+
|
75
|
+
raise TypeError, "parameter 'args[0]' (#{args[0].class}) must be an instance of Integer, or an array containing instance(s) of Integer"
|
76
|
+
end
|
77
|
+
else
|
78
|
+
|
79
|
+
args
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
case numbers.size
|
84
|
+
when 0
|
85
|
+
|
86
|
+
return self.to_s
|
87
|
+
when 1
|
88
|
+
|
89
|
+
return self.to_s.chars.to_a.reverse.each_slice(numbers[0]).map(&:join).join(',').reverse
|
90
|
+
else
|
91
|
+
|
92
|
+
reversed_chars = self.to_s.chars.to_a#.reverse
|
93
|
+
|
94
|
+
r = []
|
95
|
+
|
96
|
+
last_n = nil
|
97
|
+
|
98
|
+
numbers.each do |n|
|
99
|
+
|
100
|
+
r << separator unless r.empty?
|
101
|
+
r << reversed_chars.pop(n).reverse
|
102
|
+
|
103
|
+
last_n = n
|
104
|
+
end
|
105
|
+
|
106
|
+
until reversed_chars.empty?
|
107
|
+
|
108
|
+
r << separator unless r.empty?
|
109
|
+
r << reversed_chars.pop(last_n).reverse
|
110
|
+
end
|
111
|
+
|
112
|
+
r.flatten.join('').reverse
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end # module Kernel
|
116
|
+
|
117
|
+
# ############################## end of file ############################# #
|
118
|
+
|
@@ -1,16 +1,17 @@
|
|
1
1
|
|
2
2
|
# ######################################################################## #
|
3
|
-
# File:
|
3
|
+
# File: lib/xqsr3/extensions/io/writelines.rb
|
4
4
|
#
|
5
|
-
# Purpose:
|
5
|
+
# Purpose: Adds a writelines() method to the IO class
|
6
6
|
#
|
7
|
-
# Created:
|
8
|
-
# Updated:
|
7
|
+
# Created: 13th April 2007
|
8
|
+
# Updated: 29th March 2024
|
9
9
|
#
|
10
|
-
# Home:
|
10
|
+
# Home: http://github.com/synesissoftware/xqsr3
|
11
11
|
#
|
12
|
-
# Author:
|
12
|
+
# Author: Matthew Wilson
|
13
13
|
#
|
14
|
+
# Copyright (c) 2019-2024, Matthew Wilson and Synesis Information Systems
|
14
15
|
# Copyright (c) 2007-2019, Matthew Wilson and Synesis Software
|
15
16
|
# All rights reserved.
|
16
17
|
#
|
@@ -25,7 +26,7 @@
|
|
25
26
|
# notice, this list of conditions and the following disclaimer in the
|
26
27
|
# documentation and/or other materials provided with the distribution.
|
27
28
|
#
|
28
|
-
# * Neither the names of the copyright
|
29
|
+
# * Neither the names of the copyright holders nor the names of its
|
29
30
|
# contributors may be used to endorse or promote products derived from
|
30
31
|
# this software without specific prior written permission.
|
31
32
|
#
|
@@ -54,45 +55,44 @@ require 'xqsr3/io/writelines'
|
|
54
55
|
|
55
56
|
class IO
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
# Extends +IO+ class with the +::Xqsr3::IO::write_lines+ method
|
59
|
+
#
|
60
|
+
#
|
61
|
+
#def self.writelines(path, contents, lineSep = nil, columnSep = nil)
|
62
|
+
def self.writelines(path, contents, *args)
|
62
63
|
|
63
|
-
|
64
|
+
options = {}
|
64
65
|
|
65
|
-
|
66
|
-
|
66
|
+
case args.size
|
67
|
+
when 0
|
67
68
|
|
68
|
-
|
69
|
-
|
69
|
+
;
|
70
|
+
when 1
|
70
71
|
|
71
|
-
|
72
|
+
arg3 = args[0]
|
72
73
|
|
73
|
-
|
74
|
+
if arg3.respond_to?(:to_hash)
|
74
75
|
|
75
|
-
|
76
|
-
|
76
|
+
options.merge! arg3.to_hash
|
77
|
+
else
|
77
78
|
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
options[:line_separator] = arg3
|
80
|
+
end
|
81
|
+
when 2
|
81
82
|
|
82
|
-
|
83
|
-
|
83
|
+
arg3 = args[0]
|
84
|
+
arg4 = args[1]
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
86
|
+
options[:line_separator] = arg2
|
87
|
+
options[:column_separator] = arg2
|
88
|
+
else
|
88
89
|
|
89
|
-
|
90
|
-
|
90
|
+
raise ArgumentError, "wrong number of arguments (given #{2 + args.size}, expected 2..4)"
|
91
|
+
end
|
91
92
|
|
92
|
-
|
93
|
-
|
93
|
+
::Xqsr3::IO.writelines path, contents, **options
|
94
|
+
end
|
94
95
|
end # class IO
|
95
96
|
|
96
97
|
# ############################## end of file ############################# #
|
97
98
|
|
98
|
-
|