facets 4.1.0 → 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/HISTORY.md +82 -0
- data/README.md +1 -1
- 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_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 +5 -4
- 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/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/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/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 +4 -4
- 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/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/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 +3 -3
- 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/rotate.rb +1 -1
- data/lib/core/facets/string/similarity.rb +2 -2
- 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 +2 -0
- data/lib/core/facets/symbol/call.rb +0 -9
- data/lib/core/facets/time/change.rb +3 -3
- data/lib/core/facets/time/stamp.rb +1 -1
- 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/mirror.rb +276 -0
- data/lib/standard/facets/paramix.rb +150 -0
- metadata +7 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a5a0fe1f15e51a1eb910a6c2725ec6a40a03e34250401a1211bb7b5f2c0fb5e5
|
|
4
|
+
data.tar.gz: 45b61435e8159919b01bf88bc37a4ebe8330c1743b7a8d544763299f798957dc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5bef39a334f530b1fea26901da7975267cb7e764c2763798130d09cb457187d2c6e935261a83527673e37d510bc66869a264dc4b459e81c9d0e6519d26715ac0
|
|
7
|
+
data.tar.gz: eb3591e5256eea3e91c82a8a074830cdc7c4b415fdd13ac40da475192a8e76a93d9157c0445e0abd1595b88f26abff9a8aee541e77cf6155cc88ca0123f966d5
|
data/HISTORY.md
CHANGED
|
@@ -1,5 +1,87 @@
|
|
|
1
1
|
# Facets Release History
|
|
2
2
|
|
|
3
|
+
## 4.2.0 / 2026-11-17
|
|
4
|
+
|
|
5
|
+
This release brings back four libraries that began in Facets and were
|
|
6
|
+
later kept as gems of their own: number bases (Radix), object
|
|
7
|
+
introspection (Instance, now `Mirror`), parametric mixins (Paramix) and
|
|
8
|
+
string masks (Strmask). The number bases load with `require 'facets'`;
|
|
9
|
+
the other three are loaded on request.
|
|
10
|
+
|
|
11
|
+
Changes:
|
|
12
|
+
|
|
13
|
+
* Additions
|
|
14
|
+
|
|
15
|
+
* `Integer#to_base` and `String#from_base` convert to and from any base
|
|
16
|
+
from 2 to 62, or with your own digits: `255.to_base(62)` is `"47"`,
|
|
17
|
+
and `"47".from_base(62)` is 255. Ruby's `to_s` and `to_i` stop at
|
|
18
|
+
base 36; up to there, `to_base` gives the same result as `to_s`, and
|
|
19
|
+
`from_base` the same as `to_i` for valid input. Unlike `to_i`,
|
|
20
|
+
`from_base` raises `ArgumentError` for a character that isn't a digit.
|
|
21
|
+
In base 256 the digits are bytes: `256.to_base(256)` is `"\x01\x00"`,
|
|
22
|
+
a binary string, and `from_base(256)` reads a string's bytes.
|
|
23
|
+
* Given an array, `to_base` gives an array, with no limit on the base.
|
|
24
|
+
An array holding a number is the base, and the result is the digit
|
|
25
|
+
values: `1000.to_base([100])` is `[10, 0]`. Any other array is the
|
|
26
|
+
digits, which can be any objects: `5.to_base([:zero, :one])` is
|
|
27
|
+
`[:one, :zero, :one]`.
|
|
28
|
+
* `Array#from_base` turns such an array back into a number, given the
|
|
29
|
+
same base: `[10, 0].from_base([100])` is 1000, and
|
|
30
|
+
`[1, 0].from_base(256)` is 256. All three load with
|
|
31
|
+
`require 'facets'`, and are based on the Radix gem.
|
|
32
|
+
* `String#margin` takes a `:flush` option, which gives every line the
|
|
33
|
+
same margin and doesn't keep their relative indentation:
|
|
34
|
+
`"abc\n xyz".margin(2, flush: true)` is `" abc\n xyz"`. This is
|
|
35
|
+
what `String#tab`, removed in 3.1.0, used to do; `String#tabto` is
|
|
36
|
+
`margin(n, lead: true)`.
|
|
37
|
+
* `Mirror`, loaded with `require 'facets/mirror'`, looks at an object
|
|
38
|
+
from the outside: `obj.mirror.to_h` gives its instance variables as a
|
|
39
|
+
hash, and the mirror also gets and sets them, and gives the object's
|
|
40
|
+
class and methods. It calls Ruby's own reflection methods, so it gives
|
|
41
|
+
the true answers even for an object that redefines `class` or
|
|
42
|
+
`instance_variables`, or a `BasicObject` that has neither. The class
|
|
43
|
+
is `Kernel::Mirror`, so an application's own `Mirror` class takes
|
|
44
|
+
precedence instead of clashing. Based on the Instance gem.
|
|
45
|
+
* `Paramix`, loaded with `require 'facets/paramix'`, makes a module
|
|
46
|
+
take parameters where it is included or extended:
|
|
47
|
+
`include Greeter[:name => 'x']`. A module that includes `Paramix`
|
|
48
|
+
has a `parameterized` block, which is given the parameters and
|
|
49
|
+
defines the methods that depend on them. Paramix was part of Facets
|
|
50
|
+
until 2.7.0, then the Paramix gem, where the module to include was
|
|
51
|
+
`Paramix::Parametric`; that name still works.
|
|
52
|
+
* `String::Mask` and `String#mask`, loaded with
|
|
53
|
+
`require 'facets/string/mask'`, treat a string as a mask with empty
|
|
54
|
+
slots and combine masks character by character:
|
|
55
|
+
`("abc..123".mask('.') + "ab..789.").to_s` is `"abc.7893"`, and `-`,
|
|
56
|
+
`*`, `&` and `^` work likewise. This was Facets' `string/mask` until
|
|
57
|
+
2.7.0, then the Strmask gem. Compared with the gem: masks of
|
|
58
|
+
different lengths can be combined, a mask can be made without an
|
|
59
|
+
empty-slot character, equal masks are the same hash key, `apply`
|
|
60
|
+
works, only methods a string has are passed on to it, and a mask is
|
|
61
|
+
equal only to another mask (compare `mask.to_s` with a string).
|
|
62
|
+
|
|
63
|
+
* Bug fixes
|
|
64
|
+
|
|
65
|
+
* `String#dashcase` now squeezes runs of dashes, as its docs say:
|
|
66
|
+
`"Dash - Case".dashcase` is `"dash-case"`, not `"dash-----case"`.
|
|
67
|
+
* `Dir.multiglob` ignored `:recurse => true`. It now gives the same
|
|
68
|
+
result as `Dir.multiglob_r`, and no longer changes the options hash
|
|
69
|
+
it is given.
|
|
70
|
+
* `Array#delete_first` now returns the block's result when the item
|
|
71
|
+
isn't found, as its docs say and as `Array#delete` does.
|
|
72
|
+
* `Crypt3.generate_salt`, and so the salt `Crypt3.crypt` makes when
|
|
73
|
+
given none, now comes from `SecureRandom`. It came from `rand`, which
|
|
74
|
+
isn't a cryptographic source.
|
|
75
|
+
|
|
76
|
+
* Documentation
|
|
77
|
+
|
|
78
|
+
* The examples in the doc comments run again, with `rake qed:code`,
|
|
79
|
+
and CI now runs them. Many had gone stale: wrong results, renamed
|
|
80
|
+
methods, and output that depended on the local time zone. Those that
|
|
81
|
+
can't run, such as interactive prompts and random output, are now
|
|
82
|
+
marked as sample text. Six `@uncommon` tags named files that don't
|
|
83
|
+
exist; a test now checks those paths.
|
|
84
|
+
|
|
3
85
|
## 4.1.0 / 2026-11-08
|
|
4
86
|
|
|
5
87
|
Facets and ActiveSupport define many of the same methods, and whichever
|
data/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
**More of Ruby, one method at a time.** Facets is a collection of extensions to Ruby's core classes and standard library, plus a few small, reusable classes and modules. Most methods live in their own files, so you can load one extension, a class's extensions, or the core collection.
|
|
7
7
|
|
|
8
|
-
Facets took its name in 2004, growing out of a small methods library started in 2002, and is still maintained. The current release is **4.
|
|
8
|
+
Facets took its name in 2004, growing out of a small methods library started in 2002, and is still maintained. The current release is **4.2.0**, which requires **Ruby 3.1 or newer**. See the [release history](HISTORY.md) for changes and migration notes from earlier versions. The `main` branch also contains changes awaiting the next release.
|
|
9
9
|
|
|
10
10
|
## Install
|
|
11
11
|
|
|
@@ -5,7 +5,7 @@ class Array
|
|
|
5
5
|
# common if they return the same value for Object#hash and if obj1 == obj2.
|
|
6
6
|
#
|
|
7
7
|
# This can be useful, for instance, in determining all persons that share
|
|
8
|
-
# their last name with another person
|
|
8
|
+
# their last name with another person:
|
|
9
9
|
#
|
|
10
10
|
# persons.commonality { |person| person.last_name }
|
|
11
11
|
#
|
|
@@ -7,15 +7,16 @@ class Array
|
|
|
7
7
|
# the item is not found.
|
|
8
8
|
#
|
|
9
9
|
# a = [ "a", "b", "b", "b", "c" ]
|
|
10
|
-
# a.delete_first("b")
|
|
11
|
-
# a
|
|
12
|
-
# a.
|
|
13
|
-
# a.
|
|
10
|
+
# a.delete_first("b") #=> "b"
|
|
11
|
+
# a #=> ["a", "b", "b", "c"]
|
|
12
|
+
# a.delete_first("z") #=> nil
|
|
13
|
+
# a.delete_first("z") { "not found" } #=> "not found"
|
|
14
14
|
def delete_first(el)
|
|
15
15
|
i = index(el)
|
|
16
16
|
if i
|
|
17
17
|
delete_at(i)
|
|
18
|
+
elsif block_given?
|
|
19
|
+
yield(el)
|
|
18
20
|
end
|
|
19
21
|
end
|
|
20
22
|
end
|
|
21
|
-
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
class Array
|
|
2
|
+
|
|
3
|
+
# Reads the array as the digit values of a number in the given base,
|
|
4
|
+
# most significant first, and returns the number. Any base from 2 up
|
|
5
|
+
# works.
|
|
6
|
+
#
|
|
7
|
+
# [1, 0].from_base(256) #=> 256
|
|
8
|
+
# [100, 10].from_base(256) #=> 25610
|
|
9
|
+
# [2, 5, 5].from_base(10) #=> 255
|
|
10
|
+
#
|
|
11
|
+
# This reverses Ruby's `Integer#digits`, which gives the digit values
|
|
12
|
+
# least significant first:
|
|
13
|
+
#
|
|
14
|
+
# 255.digits(16) #=> [15, 15]
|
|
15
|
+
# 255.digits(16).reverse.from_base(16) #=> 255
|
|
16
|
+
#
|
|
17
|
+
# The base can also be given as Integer#to_base takes it for an
|
|
18
|
+
# array: as an array holding the base, or as an array of the digits,
|
|
19
|
+
# starting with the digit for zero. Whatever array `to_base` was
|
|
20
|
+
# given reads its result back.
|
|
21
|
+
#
|
|
22
|
+
# [10, 0].from_base([100]) #=> 1000
|
|
23
|
+
# %w[L O L].from_base(%w[O L T W F S]) #=> 37
|
|
24
|
+
# [:one, :zero, :one].from_base([:zero, :one]) #=> 5
|
|
25
|
+
#
|
|
26
|
+
# Raises ArgumentError for an empty array, and for an element that
|
|
27
|
+
# isn't a digit of the base: an Integer from 0 to one less than the
|
|
28
|
+
# base, or one of the digits given.
|
|
29
|
+
#
|
|
30
|
+
# String#from_base reads a number written as a string.
|
|
31
|
+
#
|
|
32
|
+
# Since: 4.2.0
|
|
33
|
+
|
|
34
|
+
def from_base(base)
|
|
35
|
+
values = nil
|
|
36
|
+
case base
|
|
37
|
+
when ::Integer
|
|
38
|
+
# digit values
|
|
39
|
+
when ::Array
|
|
40
|
+
if base.size == 1 && base.first.is_a?(::Integer)
|
|
41
|
+
base = base.first
|
|
42
|
+
else
|
|
43
|
+
if base.size < 2
|
|
44
|
+
raise ArgumentError, "a base needs at least two digits"
|
|
45
|
+
end
|
|
46
|
+
if base.uniq.size < base.size
|
|
47
|
+
raise ArgumentError, "digits must not repeat"
|
|
48
|
+
end
|
|
49
|
+
values = {}
|
|
50
|
+
base.each_with_index { |d, i| values[d] = i }
|
|
51
|
+
base = base.size
|
|
52
|
+
end
|
|
53
|
+
else
|
|
54
|
+
raise TypeError, "base must be an Integer or Array, not #{base.class}"
|
|
55
|
+
end
|
|
56
|
+
raise ArgumentError, "invalid radix #{base}" if base < 2
|
|
57
|
+
raise ArgumentError, "no digits" if empty?
|
|
58
|
+
|
|
59
|
+
inject(0) do |n, d|
|
|
60
|
+
v = values ? values[d] : d
|
|
61
|
+
unless v.is_a?(::Integer) && v >= 0 && v < base
|
|
62
|
+
raise ArgumentError, "invalid digit #{d.inspect} for base #{base}"
|
|
63
|
+
end
|
|
64
|
+
n * base + v
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
end
|
|
@@ -87,7 +87,7 @@ class Enumerator::Lazy
|
|
|
87
87
|
# If neither a block nor argument is given, an Enumerator for _all_ indexes is returned.
|
|
88
88
|
#
|
|
89
89
|
# enum = (42 .. Float::INFINITY).lazy.indexes
|
|
90
|
-
# #=>
|
|
90
|
+
# enum.class #=> Enumerator::Lazy
|
|
91
91
|
# enum.next #=> 0
|
|
92
92
|
# enum.next #=> 1
|
|
93
93
|
# enum.first(5) #=> [0, 1, 2, 3, 4]
|
|
@@ -5,10 +5,10 @@ class Array
|
|
|
5
5
|
#
|
|
6
6
|
# Examples
|
|
7
7
|
#
|
|
8
|
-
# ['a1','a2','b1','a3','b2','a4'].
|
|
9
|
-
# #=> [['a1','a2'],b1,['a3','b2','a4']]
|
|
8
|
+
# ['a1','a2','b1','a3','b2','a4'].split_at('b1')
|
|
9
|
+
# #=> [['a1','a2'],'b1',['a3','b2','a4']]
|
|
10
10
|
#
|
|
11
|
-
# ['a1','a2','b1','a3','b2','a4'].
|
|
11
|
+
# ['a1','a2','b1','a3','b2','a4'].split_at('foo')
|
|
12
12
|
# #=> [[],nil,[]]
|
|
13
13
|
#
|
|
14
14
|
# Returns list of split-up arrays. [Tuple<Array,Object,Array>]
|
data/lib/core/facets/array.rb
CHANGED
|
@@ -16,6 +16,7 @@ require_relative 'array/each_pair.rb'
|
|
|
16
16
|
require_relative 'array/each_value.rb'
|
|
17
17
|
require_relative 'array/entropy.rb'
|
|
18
18
|
require_relative 'array/from.rb'
|
|
19
|
+
require_relative 'array/from_base.rb'
|
|
19
20
|
require_relative 'array/indexable.rb'
|
|
20
21
|
#require_relative 'array/intersection.rb' # too new
|
|
21
22
|
#require_relative 'array/median.rb' # uncommon
|
data/lib/core/facets/boolean.rb
CHANGED
|
@@ -69,7 +69,7 @@ end
|
|
|
69
69
|
class String
|
|
70
70
|
# Interpret common affirmative string meanings as true,
|
|
71
71
|
# otherwise nil or false. Blank space and case are ignored.
|
|
72
|
-
# The following strings that will return true
|
|
72
|
+
# The following strings that will return true:
|
|
73
73
|
#
|
|
74
74
|
# true
|
|
75
75
|
# yes
|
|
@@ -79,7 +79,7 @@ class String
|
|
|
79
79
|
# y
|
|
80
80
|
# ==
|
|
81
81
|
#
|
|
82
|
-
# The following strings will return nil
|
|
82
|
+
# The following strings will return nil:
|
|
83
83
|
#
|
|
84
84
|
# nil
|
|
85
85
|
# null
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
class Class
|
|
2
2
|
|
|
3
|
-
# List all
|
|
3
|
+
# List all descendants of this class. The order differs between
|
|
4
|
+
# Rubies, so these examples sort the list.
|
|
4
5
|
#
|
|
5
6
|
# class A ; end
|
|
6
7
|
# class B < A; end
|
|
7
8
|
# class C < A; end
|
|
8
|
-
# A.descendants #=> [B,C]
|
|
9
|
+
# A.descendants.sort_by(&:name) #=> [B,C]
|
|
9
10
|
#
|
|
10
11
|
# You may also limit the generational distance the subclass may be from
|
|
11
12
|
# the parent class.
|
|
@@ -13,8 +14,8 @@ class Class
|
|
|
13
14
|
# class X ; end
|
|
14
15
|
# class Y < X; end
|
|
15
16
|
# class Z < Y; end
|
|
16
|
-
# X.descendants
|
|
17
|
-
# X.descendants(1)
|
|
17
|
+
# X.descendants.sort_by(&:name) #=> [Y,Z]
|
|
18
|
+
# X.descendants(1) #=> [Y]
|
|
18
19
|
#
|
|
19
20
|
# Ruby 4.1 adds its own Module#descendants. Where Ruby has it, a call
|
|
20
21
|
# without a limit is passed to Ruby's, and Facets only handles the
|
|
@@ -5,7 +5,7 @@ class Dir
|
|
|
5
5
|
# Dir.multiglob('tmp/*.rb', 'tmp/*.py')
|
|
6
6
|
#
|
|
7
7
|
# Rather then constants for options multiglob accepts a trailing options
|
|
8
|
-
# hash of symbol keys
|
|
8
|
+
# hash of symbol keys:
|
|
9
9
|
#
|
|
10
10
|
# :noescape File::FNM_NOESCAPE
|
|
11
11
|
# :casefold File::FNM_CASEFOLD
|
|
@@ -13,7 +13,7 @@ class Dir
|
|
|
13
13
|
# :dotmatch File::FNM_DOTMATCH
|
|
14
14
|
# :strict File::FNM_PATHNAME && File::FNM_DOTMATCH
|
|
15
15
|
#
|
|
16
|
-
# It also has an option for recurse
|
|
16
|
+
# It also has an option for recurse:
|
|
17
17
|
#
|
|
18
18
|
# :recurse Recurively include contents of directories.
|
|
19
19
|
#
|
|
@@ -26,11 +26,10 @@ class Dir
|
|
|
26
26
|
# Dir.multiglob('tmp/**/*')
|
|
27
27
|
#
|
|
28
28
|
def self.multiglob(*patterns)
|
|
29
|
-
options = (Hash === patterns.last ? patterns.pop : {})
|
|
29
|
+
options = (Hash === patterns.last ? patterns.pop.dup : {})
|
|
30
30
|
|
|
31
31
|
if options.delete(:recurse)
|
|
32
|
-
|
|
33
|
-
multiglob_r(*patterns)
|
|
32
|
+
return multiglob_r(*patterns)
|
|
34
33
|
end
|
|
35
34
|
|
|
36
35
|
bitflags = 0
|
|
@@ -41,10 +40,6 @@ class Dir
|
|
|
41
40
|
|
|
42
41
|
patterns = [patterns].flatten.compact
|
|
43
42
|
|
|
44
|
-
if options[:recurse]
|
|
45
|
-
patterns += patterns.collect{ |f| File.join(f, '**', '**') }
|
|
46
|
-
end
|
|
47
|
-
|
|
48
43
|
files = []
|
|
49
44
|
files += patterns.collect{ |pattern| Dir.glob(pattern, bitflags) }.flatten.uniq
|
|
50
45
|
|
|
@@ -10,9 +10,9 @@ module Enumerable
|
|
|
10
10
|
# r #=> 6
|
|
11
11
|
#
|
|
12
12
|
def apply
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
##Functor.new do |sym, *args, &blk|
|
|
14
|
+
## inject{ |r, e| r.__send__(sym, e, *args, &blk) }
|
|
15
|
+
##end
|
|
16
16
|
per(:inject)
|
|
17
17
|
end
|
|
18
18
|
|
|
@@ -3,10 +3,10 @@ module Enumerable
|
|
|
3
3
|
# Clusters together adjacent elements into a list of sub-arrays.
|
|
4
4
|
#
|
|
5
5
|
# [2,2,2,3,3,4,2,2,1].cluster{ |x| x }
|
|
6
|
-
#
|
|
6
|
+
# #=> [[2, 2, 2], [3, 3], [4], [2, 2], [1]]
|
|
7
7
|
#
|
|
8
8
|
# ["dog", "duck", "cat", "dude"].cluster{ |x| x[0] }
|
|
9
|
-
#
|
|
9
|
+
# #=> [["dog", "duck"], ["cat"], ["dude"]]
|
|
10
10
|
#
|
|
11
11
|
# @author Oleg K
|
|
12
12
|
|
|
@@ -12,10 +12,10 @@ module Enumerable
|
|
|
12
12
|
# Here are some (but not a complete set of) examples.
|
|
13
13
|
#
|
|
14
14
|
# If the enumerable is a collection of perfect pairs, like that
|
|
15
|
-
# which Hash#to_a generates, then #
|
|
15
|
+
# which Hash#to_a generates, then #merge can be used.
|
|
16
16
|
#
|
|
17
17
|
# a = [ [:a,1], [:b,2] ]
|
|
18
|
-
# a.hashify.
|
|
18
|
+
# a.hashify.merge #=> { :a=>1, :b=>2 }
|
|
19
19
|
#
|
|
20
20
|
# If it it contains only arrays, but are not perfect pairs,
|
|
21
21
|
# then #concat can be used.
|
|
@@ -63,7 +63,7 @@ module Enumerable
|
|
|
63
63
|
# of elements.
|
|
64
64
|
#
|
|
65
65
|
# a = [:a,1,:b,2,:c]
|
|
66
|
-
# a.
|
|
66
|
+
# a.hashify.splat #=> { :a=>1, :b=>2, :c=>nil }
|
|
67
67
|
def splat
|
|
68
68
|
a = @enum.to_a
|
|
69
69
|
a << nil if a.size.odd?
|
|
@@ -75,7 +75,7 @@ module Enumerable
|
|
|
75
75
|
# of elements.
|
|
76
76
|
#
|
|
77
77
|
# a = [:a,1,[:b,2,:c]]
|
|
78
|
-
# a.
|
|
78
|
+
# a.hashify.flat #=> { :a=>1, :b=>2, :c=>nil }
|
|
79
79
|
def flat
|
|
80
80
|
a = @enum.to_a.flatten
|
|
81
81
|
a << nil if a.size.odd?
|
|
@@ -141,7 +141,7 @@ module Enumerable
|
|
|
141
141
|
|
|
142
142
|
# Convert enumerable object to Hash using index as keys.
|
|
143
143
|
#
|
|
144
|
-
# [:a, :b, :c].hashify.by_index #=> {
|
|
144
|
+
# [:a, :b, :c].hashify.by_index #=> {0=>:a, 1=>:b, 2=>:c}
|
|
145
145
|
#
|
|
146
146
|
def by_index
|
|
147
147
|
h = {}
|
|
@@ -27,13 +27,13 @@ module Enumerable
|
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
# Allows one to know the position of the enumeration easily
|
|
30
|
+
# Allows one to know the position of the enumeration easily:
|
|
31
31
|
#
|
|
32
32
|
# 5.times.collect.with_position do |i, position|
|
|
33
33
|
# $stdout.write "#{i}#{position.middle? ? ',' : ''}"
|
|
34
34
|
# end
|
|
35
35
|
#
|
|
36
|
-
# Where this becomes more useful, is when it's easy to determine the position of elements, e.g
|
|
36
|
+
# Where this becomes more useful, is when it's easy to determine the position of elements, e.g.:
|
|
37
37
|
#
|
|
38
38
|
# 10.times.select(&:odd?).with_position{|n,p| ... }
|
|
39
39
|
#
|
|
@@ -11,17 +11,17 @@ class Enumerator
|
|
|
11
11
|
end
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
## Old 1.8 version (left temporarily for reference)
|
|
15
|
+
##
|
|
16
|
+
## def fx
|
|
17
|
+
## Functor.new(&method(:fx_send).to_proc)
|
|
18
|
+
## end
|
|
19
|
+
##
|
|
20
|
+
## private
|
|
21
|
+
##
|
|
22
|
+
## def fx_send(op, *a, &b)
|
|
23
|
+
## map{ |e| e.send(op, *a, &b) }
|
|
24
|
+
## end
|
|
25
25
|
|
|
26
26
|
end
|
|
27
27
|
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
# *essential* we mean, more or less, the extensions we think
|
|
4
4
|
# ought to be in Ruby proper anyway.
|
|
5
5
|
#
|
|
6
|
-
# NOTE: This is very much a work in progress! Please feel
|
|
7
|
-
#
|
|
6
|
+
# NOTE: This is very much a work in progress! Please feel free to
|
|
7
|
+
# suggest which extensions you think belong here.
|
|
8
8
|
|
|
9
9
|
require_relative 'functor'
|
|
10
10
|
|
|
@@ -8,7 +8,7 @@ class File
|
|
|
8
8
|
# file, copying the file back to the original on completion.
|
|
9
9
|
#
|
|
10
10
|
# @uncommon
|
|
11
|
-
# require 'facets/
|
|
11
|
+
# require 'facets/file/atomic_open'
|
|
12
12
|
#
|
|
13
13
|
def self.atomic_open(file_name, mode="r", temp_dir=nil, &block)
|
|
14
14
|
temp_dir = temp_dir || Dir.tmpdir
|
|
@@ -13,7 +13,7 @@ class File
|
|
|
13
13
|
# binary mode (no mapping of the plattform's newlines to "\n" is
|
|
14
14
|
# done) or to append to it.
|
|
15
15
|
#
|
|
16
|
-
# Assuming we had a file 'message.txt' and had a binary file 'binary.dat'
|
|
16
|
+
# Assuming we had a file 'message.txt' and had a binary file 'binary.dat':
|
|
17
17
|
#
|
|
18
18
|
# # Reverse contents of "message.txt"
|
|
19
19
|
# File.rewrite("message.txt") { |str| str.reverse }
|
|
@@ -53,13 +53,13 @@ class File
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
# In place version of #rewrite. This version of method requires that the
|
|
56
|
-
# string be modified in place within the block
|
|
56
|
+
# string be modified in place within the block:
|
|
57
57
|
#
|
|
58
58
|
# # Reverse contents of "message"
|
|
59
|
-
# File.rewrite("message.txt") { |str| str.reverse! }
|
|
59
|
+
# File.rewrite!("message.txt") { |str| str.reverse! }
|
|
60
60
|
#
|
|
61
61
|
# # Replace "foo" by "bar" in "binary"
|
|
62
|
-
# File.rewrite("binary.dat", "b") { |str| str.gsub!("foo", "bar") }
|
|
62
|
+
# File.rewrite!("binary.dat", "b") { |str| str.gsub!("foo", "bar") }
|
|
63
63
|
#
|
|
64
64
|
def self.rewrite!(name, mode = "") #:yield:
|
|
65
65
|
unless block_given?
|
|
@@ -22,7 +22,7 @@ class Hash
|
|
|
22
22
|
#
|
|
23
23
|
# foo = { :person=>{ :name=>'Gavin', :wife=>:Lisa } }
|
|
24
24
|
# foo.deep_rekey{ |k| k.to_s } #=> { "person"=>{ "name"=>"Gavin", "wife"=>:Lisa } }
|
|
25
|
-
# foo #=> {
|
|
25
|
+
# foo #=> { :person=>{ :name=>'Gavin', :wife=>:Lisa } }
|
|
26
26
|
#
|
|
27
27
|
# If no key map or block is given, then all keys are converted
|
|
28
28
|
# to Symbols.
|
|
@@ -14,12 +14,12 @@ class Hash
|
|
|
14
14
|
self
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
##def <<(pairs)
|
|
18
|
+
## if Array === pairs
|
|
19
|
+
## merge! Hash[*pairs]
|
|
20
|
+
## else
|
|
21
|
+
## merge! pairs
|
|
22
|
+
## end
|
|
23
|
+
##end
|
|
24
24
|
|
|
25
25
|
end
|
|
@@ -50,13 +50,13 @@ class Hash
|
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
else
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
##hash = dup.clear # to keep default_proc
|
|
54
|
+
##(keys - key_map.keys).each do |key|
|
|
55
|
+
## hash[key] = self[key]
|
|
56
|
+
##end
|
|
57
|
+
##key_map.each do |from, to|
|
|
58
|
+
## hash[to] = self[from] if key?(from)
|
|
59
|
+
##end
|
|
60
60
|
hash = dup # to keep default_proc
|
|
61
61
|
key_map.each_pair do |from, to|
|
|
62
62
|
hash[to] = hash.delete(from) if hash.key?(from)
|
|
@@ -6,7 +6,7 @@ class Hash
|
|
|
6
6
|
#
|
|
7
7
|
# Two hashes are weaved together to produce a new hash.
|
|
8
8
|
# The two hashes need to be compatible according to the
|
|
9
|
-
# following rules for each node:
|
|
9
|
+
# following rules for each node:
|
|
10
10
|
#
|
|
11
11
|
# hash, hash => hash (recursive +)
|
|
12
12
|
# hash, array => error
|