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
|
@@ -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?
|
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
class Hash
|
|
2
2
|
|
|
3
|
-
#
|
|
3
|
+
# Ruby's own Hash#count, from Enumerable, counts key-value pairs:
|
|
4
4
|
#
|
|
5
|
-
# {:A=>1, :B=>1}.count(1) #=>
|
|
5
|
+
# {:A=>1, :B=>1}.count([:A, 1]) #=> 1
|
|
6
6
|
#
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
# Facets' version used to count values instead, which changed Ruby's
|
|
8
|
+
# answer for every library in the program. It now returns Ruby's answer,
|
|
9
|
+
# and warns when given something that can't be a key-value pair, since
|
|
10
|
+
# that's how the old Facets version was called. To count values, use
|
|
11
|
+
# `hash.values.count(value)`.
|
|
12
|
+
#
|
|
13
|
+
# {:A=>1, :B=>1}.values.count(1) #=> 2
|
|
14
|
+
#
|
|
15
|
+
# @deprecated Facets no longer changes Hash#count. This warning wrapper is
|
|
16
|
+
# scheduled for removal after 2027-09-30.
|
|
17
|
+
#
|
|
18
|
+
def count(*args, &block)
|
|
19
|
+
if args.size == 1 && !(Array === args.first && args.first.size == 2)
|
|
20
|
+
warn "Facets' Hash#count(value) is deprecated. Hash#count now counts key-value pairs, " \
|
|
21
|
+
"as Ruby's own does, so this returns 0. To count values, use hash.values.count(value). " \
|
|
22
|
+
"It will be removed after 2027-09-30.", uplevel: 1
|
|
12
23
|
end
|
|
24
|
+
super
|
|
13
25
|
end
|
|
14
26
|
|
|
15
27
|
end
|
|
16
|
-
|
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
class Hash
|
|
2
2
|
|
|
3
3
|
# Same as Hash#merge but recursively merges sub-hashes.
|
|
4
|
+
#
|
|
5
|
+
# {a: {b: 1, c: 2}}.deep_merge(a: {b: 3}) #=> {a: {b: 3, c: 2}}
|
|
6
|
+
#
|
|
7
|
+
# Like Hash#merge, it takes a block to decide the value of a key both
|
|
8
|
+
# hashes have, other than one that is itself merged.
|
|
9
|
+
#
|
|
10
|
+
# {a: {b: 1}}.deep_merge(a: {b: 2}) { |key, mine, theirs| mine + theirs }
|
|
11
|
+
# #=> {a: {b: 3}}
|
|
12
|
+
#
|
|
13
|
+
# A sub-hash is merged only when the other value is a hash too. This
|
|
14
|
+
# matches ActiveSupport's Hash#deep_merge, so the two agree whichever is
|
|
15
|
+
# loaded, and it honors ActiveSupport's #deep_merge? when that's defined.
|
|
4
16
|
|
|
5
|
-
def deep_merge(other)
|
|
6
|
-
|
|
7
|
-
other.each do |key, value|
|
|
8
|
-
myval = self[key]
|
|
9
|
-
if value.is_a?(Hash) && myval.is_a?(Hash)
|
|
10
|
-
hash[key] = myval.deep_merge(value)
|
|
11
|
-
else
|
|
12
|
-
hash[key] = value
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
hash
|
|
17
|
+
def deep_merge(other, &block)
|
|
18
|
+
dup.deep_merge!(other, &block)
|
|
16
19
|
end
|
|
17
20
|
|
|
18
|
-
# Same as Hash#merge! but recursively merges sub-hashes.
|
|
21
|
+
# Same as Hash#merge! but recursively merges sub-hashes. The sub-hashes
|
|
22
|
+
# themselves aren't changed; merged copies replace them.
|
|
19
23
|
|
|
20
|
-
def deep_merge!(other)
|
|
21
|
-
other
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
def deep_merge!(other, &block)
|
|
25
|
+
merge!(other) do |key, mine, theirs|
|
|
26
|
+
deep = Hash === mine &&
|
|
27
|
+
(mine.respond_to?(:deep_merge?) ? mine.deep_merge?(theirs) : Hash === theirs)
|
|
28
|
+
if deep
|
|
29
|
+
mine.deep_merge(theirs, &block)
|
|
30
|
+
elsif block
|
|
31
|
+
block.call(key, mine, theirs)
|
|
25
32
|
else
|
|
26
|
-
|
|
33
|
+
theirs
|
|
27
34
|
end
|
|
28
35
|
end
|
|
29
|
-
self
|
|
30
36
|
end
|
|
31
37
|
|
|
32
38
|
end
|
|
33
|
-
|
|
@@ -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
|
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
class Integer
|
|
2
2
|
|
|
3
|
+
# The number with its English ordinal suffix.
|
|
4
|
+
#
|
|
5
|
+
# 1.ordinal #=> "1st"
|
|
6
|
+
# 12.ordinal #=> "12th"
|
|
7
|
+
# 103.ordinal #=> "103rd"
|
|
8
|
+
# -1.ordinal #=> "-1st"
|
|
9
|
+
#
|
|
3
10
|
def ordinal
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
when 1
|
|
9
|
-
"#{self}st"
|
|
10
|
-
when 2
|
|
11
|
-
"#{self}nd"
|
|
12
|
-
when 3
|
|
13
|
-
"#{self}rd"
|
|
11
|
+
n = abs % 100
|
|
12
|
+
suffix =
|
|
13
|
+
if (11..13).include?(n)
|
|
14
|
+
'th'
|
|
14
15
|
else
|
|
15
|
-
|
|
16
|
+
case n % 10
|
|
17
|
+
when 1 then 'st'
|
|
18
|
+
when 2 then 'nd'
|
|
19
|
+
when 3 then 'rd'
|
|
20
|
+
else 'th'
|
|
21
|
+
end
|
|
16
22
|
end
|
|
17
|
-
|
|
23
|
+
"#{self}#{suffix}"
|
|
18
24
|
end
|
|
19
25
|
|
|
20
26
|
# Rails term.
|
|
21
27
|
alias_method :ordinalize, :ordinal
|
|
22
28
|
|
|
23
29
|
end
|
|
24
|
-
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
class Integer
|
|
2
|
+
|
|
3
|
+
# Converts the integer to a string in any base from 2 to 62, or
|
|
4
|
+
# with a custom set of digits.
|
|
5
|
+
#
|
|
6
|
+
# Ruby's own `to_s(base)` stops at base 36. Above that, the digits are
|
|
7
|
+
# 0-9, then a-z, then A-Z. Up to base 36, the result is the same as
|
|
8
|
+
# `to_s(base)`.
|
|
9
|
+
#
|
|
10
|
+
# 255.to_base(16) #=> "ff"
|
|
11
|
+
# 255.to_base(62) #=> "47"
|
|
12
|
+
# 809145531659995.to_base(62) #=> "3HLszsQsP"
|
|
13
|
+
# -255.to_base(62) #=> "-47"
|
|
14
|
+
#
|
|
15
|
+
# To use other digits, give them as a string, starting with the digit
|
|
16
|
+
# for zero. The number of digits is the base.
|
|
17
|
+
#
|
|
18
|
+
# 37.to_base("OLTWFS") #=> "LOL"
|
|
19
|
+
#
|
|
20
|
+
# In base 256 the digits are bytes, so the result is the number's
|
|
21
|
+
# bytes, most significant first, as a binary string.
|
|
22
|
+
#
|
|
23
|
+
# 256.to_base(256) #=> "\x01\x00"
|
|
24
|
+
# 0.to_base(256) #=> "\x00"
|
|
25
|
+
#
|
|
26
|
+
# Combining the two encodes binary data, as with Base58:
|
|
27
|
+
#
|
|
28
|
+
# b58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
|
29
|
+
# "hello".from_base(256).to_base(b58) #=> "Cn8eVZg"
|
|
30
|
+
# "Cn8eVZg".from_base(b58).to_base(256) #=> "hello"
|
|
31
|
+
#
|
|
32
|
+
# This goes through a number, so leading zero bytes are lost:
|
|
33
|
+
# "\x00hi" comes back as "hi". Formats such as Bitcoin's Base58 keep
|
|
34
|
+
# them with their own rules, which this doesn't follow.
|
|
35
|
+
#
|
|
36
|
+
# A negative number gets a leading "-", unless "-" is one of the
|
|
37
|
+
# digits, in which case it raises ArgumentError. That includes base
|
|
38
|
+
# 256, where "-" is byte 45.
|
|
39
|
+
#
|
|
40
|
+
# Given an array, the result is an array, with one element for each
|
|
41
|
+
# digit, most significant first. An array holding one number is a
|
|
42
|
+
# base, of any size from 2 up, and the result is the digit values:
|
|
43
|
+
#
|
|
44
|
+
# 1000.to_base([100]) #=> [10, 0]
|
|
45
|
+
# 256.to_base([256]) #=> [1, 0]
|
|
46
|
+
# 255.to_base([16]) #=> [15, 15]
|
|
47
|
+
#
|
|
48
|
+
# Any other array is the digits themselves, starting with the digit
|
|
49
|
+
# for zero. They can be any objects:
|
|
50
|
+
#
|
|
51
|
+
# 37.to_base(%w[O L T W F S]) #=> ["L", "O", "L"]
|
|
52
|
+
# 37.to_base(%w[O L T W F S]).join #=> "LOL"
|
|
53
|
+
# 5.to_base([:zero, :one]) #=> [:one, :zero, :one]
|
|
54
|
+
#
|
|
55
|
+
# An array can't show a sign, so a negative number raises
|
|
56
|
+
# ArgumentError.
|
|
57
|
+
#
|
|
58
|
+
# String#from_base converts a string back, and Array#from_base an
|
|
59
|
+
# array.
|
|
60
|
+
#
|
|
61
|
+
# Since: 4.2.0
|
|
62
|
+
|
|
63
|
+
def to_base(base)
|
|
64
|
+
case base
|
|
65
|
+
when ::Integer
|
|
66
|
+
if base == 256
|
|
67
|
+
if negative?
|
|
68
|
+
raise ArgumentError, "can't show a negative number in base 256, where '-' is a digit"
|
|
69
|
+
end
|
|
70
|
+
hex = to_s(16)
|
|
71
|
+
hex = "0#{hex}" if hex.size.odd?
|
|
72
|
+
return [hex].pack('H*')
|
|
73
|
+
end
|
|
74
|
+
unless base >= 2 && base <= 62
|
|
75
|
+
hint = base > 62 ? "; for digit values use to_base([#{base}])" : ""
|
|
76
|
+
raise ArgumentError, "invalid radix #{base}#{hint}"
|
|
77
|
+
end
|
|
78
|
+
return to_s(base) if base <= 36
|
|
79
|
+
digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[0, base]
|
|
80
|
+
when ::String
|
|
81
|
+
digits = base.chars
|
|
82
|
+
if digits.size < 2
|
|
83
|
+
raise ArgumentError, "a base needs at least two digits"
|
|
84
|
+
end
|
|
85
|
+
if digits.uniq.size < digits.size
|
|
86
|
+
raise ArgumentError, "digits must not repeat"
|
|
87
|
+
end
|
|
88
|
+
if negative? && digits.include?('-')
|
|
89
|
+
raise ArgumentError, "can't show a negative number when '-' is a digit"
|
|
90
|
+
end
|
|
91
|
+
digits = base
|
|
92
|
+
when ::Array
|
|
93
|
+
if negative?
|
|
94
|
+
raise ArgumentError, "can't show a negative number as an array"
|
|
95
|
+
end
|
|
96
|
+
if base.size == 1 && base.first.is_a?(::Integer)
|
|
97
|
+
raise ArgumentError, "invalid radix #{base.first}" if base.first < 2
|
|
98
|
+
return digits(base.first).reverse
|
|
99
|
+
end
|
|
100
|
+
if base.size < 2
|
|
101
|
+
raise ArgumentError, "a base needs at least two digits"
|
|
102
|
+
end
|
|
103
|
+
if base.uniq.size < base.size
|
|
104
|
+
raise ArgumentError, "digits must not repeat"
|
|
105
|
+
end
|
|
106
|
+
return digits(base.size).reverse.map { |d| base[d] }
|
|
107
|
+
else
|
|
108
|
+
raise TypeError, "base must be an Integer, String or Array, not #{base.class}"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
s = abs.digits(digits.size).reverse.map { |d| digits[d] }.join
|
|
112
|
+
negative? ? "-#{s}" : s
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end
|
data/lib/core/facets/integer.rb
CHANGED
|
@@ -4,15 +4,15 @@ module Kernel
|
|
|
4
4
|
|
|
5
5
|
# Very simple convenience method to get user input
|
|
6
6
|
# via the console. A prompt will be sent to $stdout,
|
|
7
|
-
# if given, and the input taken from $stdin
|
|
7
|
+
# if given, and the input taken from $stdin:
|
|
8
8
|
#
|
|
9
9
|
# ask "Are you happy? [Yn]"
|
|
10
10
|
#
|
|
11
|
-
# On the command line one would see
|
|
11
|
+
# On the command line one would see:
|
|
12
12
|
#
|
|
13
13
|
# Are you happy? [Yn]
|
|
14
14
|
#
|
|
15
|
-
# Responding
|
|
15
|
+
# Responding:
|
|
16
16
|
#
|
|
17
17
|
# Are you happy? [Yn] Y <ENTER>
|
|
18
18
|
#
|
|
@@ -2,11 +2,11 @@ module Kernel
|
|
|
2
2
|
# An object is blank if it's nil, empty, or a whitespace string.
|
|
3
3
|
# For example, "", " ", nil, [], and {} are blank.
|
|
4
4
|
#
|
|
5
|
-
# This simplifies
|
|
5
|
+
# This simplifies:
|
|
6
6
|
#
|
|
7
7
|
# if !address.nil? && !address.empty?
|
|
8
8
|
#
|
|
9
|
-
# to
|
|
9
|
+
# to:
|
|
10
10
|
#
|
|
11
11
|
# if !address.blank?
|
|
12
12
|
#
|
|
@@ -25,13 +25,13 @@ module Kernel
|
|
|
25
25
|
#
|
|
26
26
|
# This is handy for any representation of objects where blank is the same
|
|
27
27
|
# as not present at all. For example, this simplifies a common check for
|
|
28
|
-
# HTTP POST/query parameters
|
|
28
|
+
# HTTP POST/query parameters:
|
|
29
29
|
#
|
|
30
30
|
# state = params[:state] if params[:state].present?
|
|
31
31
|
# country = params[:country] if params[:country].present?
|
|
32
32
|
# region = state || country || 'US'
|
|
33
33
|
#
|
|
34
|
-
# becomes
|
|
34
|
+
# becomes:
|
|
35
35
|
#
|
|
36
36
|
# region = params[:state].presence || params[:country].presence || 'US'
|
|
37
37
|
#
|
|
@@ -67,12 +67,17 @@ class Hash
|
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
class String
|
|
70
|
-
# Is this string just whitespace?
|
|
70
|
+
# Is this string empty or just whitespace? Unicode whitespace, such as a
|
|
71
|
+
# non-breaking space, counts too, as it does in ActiveSupport.
|
|
71
72
|
#
|
|
72
|
-
# "abc".blank?
|
|
73
|
-
# " ".blank?
|
|
73
|
+
# "abc".blank? #=> false
|
|
74
|
+
# " ".blank? #=> true
|
|
75
|
+
# " ".blank? #=> true
|
|
74
76
|
def blank?
|
|
75
|
-
|
|
77
|
+
empty? || /\A[[:space:]]*\z/.match?(self)
|
|
78
|
+
rescue Encoding::CompatibilityError
|
|
79
|
+
# e.g. UTF-16 text, which a UTF-8 regexp can't match
|
|
80
|
+
Regexp.new('\A[[:space:]]*\z'.encode(encoding), Regexp::FIXEDENCODING).match?(self)
|
|
76
81
|
end
|
|
77
82
|
end
|
|
78
83
|
|
|
@@ -5,11 +5,11 @@ module Kernel
|
|
|
5
5
|
|
|
6
6
|
# Returns the call stack as an array of [file, lineno, method] entries.
|
|
7
7
|
#
|
|
8
|
-
# For example, from irb
|
|
8
|
+
# For example, from irb:
|
|
9
9
|
#
|
|
10
10
|
# callstack(1)
|
|
11
11
|
#
|
|
12
|
-
# _produces_
|
|
12
|
+
# _produces_:
|
|
13
13
|
#
|
|
14
14
|
# [["(irb)", 2, :irb_binding], ...]
|
|
15
15
|
#
|
data/lib/core/facets/kernel/d.rb
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
module Kernel
|
|
2
2
|
|
|
3
|
-
# Like #p but gives file and line number
|
|
3
|
+
# Like #p but gives file and line number:
|
|
4
4
|
#
|
|
5
5
|
# d("hi")
|
|
6
6
|
#
|
|
7
|
-
# produces
|
|
7
|
+
# produces:
|
|
8
8
|
#
|
|
9
|
-
# "hi"
|
|
9
|
+
# ["hi"] /home/dave/projects/foo.rb:38:in '<main>'
|
|
10
10
|
#
|
|
11
11
|
def d(*x)
|
|
12
12
|
puts "#{x.inspect} #{caller[0]}"
|
|
@@ -4,13 +4,13 @@ module Kernel
|
|
|
4
4
|
# Preferably just a block and no string.
|
|
5
5
|
|
|
6
6
|
# For debugging and showing examples. Currently this
|
|
7
|
-
# takes an argument of a string in a block
|
|
7
|
+
# takes an argument of a string in a block:
|
|
8
8
|
#
|
|
9
9
|
# demo {%{ a = [1,2,3] }}
|
|
10
10
|
# demo {%{ a.slice(1,2) }}
|
|
11
11
|
# demo {%{ a.map { |x| x**3 } }}
|
|
12
12
|
#
|
|
13
|
-
# produces
|
|
13
|
+
# produces:
|
|
14
14
|
#
|
|
15
15
|
# a = [1,2,3] #=> [1, 2, 3]
|
|
16
16
|
# a.slice(1,2) #=> [2, 3]
|
|
@@ -2,6 +2,12 @@ module Kernel
|
|
|
2
2
|
|
|
3
3
|
extend_method = instance_method(:extend)
|
|
4
4
|
|
|
5
|
+
# Removing Ruby's #extend before defining Facets' keeps `ruby -w` from
|
|
6
|
+
# warning that it was redefined; Facets calls it through extend_method.
|
|
7
|
+
# (A module prepended to Kernel would be simpler, but TruffleRuby doesn't
|
|
8
|
+
# apply it to classes that already include Kernel, such as Object.)
|
|
9
|
+
remove_method :extend
|
|
10
|
+
|
|
5
11
|
# Extend an object with a module.
|
|
6
12
|
#
|
|
7
13
|
# If a block is given, it will be evaluated into an anonymous
|
|
@@ -15,14 +21,24 @@ module Kernel
|
|
|
15
21
|
#
|
|
16
22
|
# obj.foo #=> "foo"
|
|
17
23
|
#
|
|
24
|
+
# Without a block it is Ruby's own #extend.
|
|
25
|
+
#
|
|
18
26
|
# NOTE: This is one of the few core overrides in Facets.
|
|
19
27
|
#
|
|
28
|
+
# @deprecated The block form is deprecated, so that Facets no longer has
|
|
29
|
+
# to override Ruby's #extend. Use `obj.extend(Module.new { ... })`
|
|
30
|
+
# instead. Scheduled for removal after 2027-09-30. Without Facets,
|
|
31
|
+
# Ruby raises if given only a block, and ignores a block given with
|
|
32
|
+
# modules.
|
|
33
|
+
#
|
|
20
34
|
# @author Trans
|
|
21
35
|
# @author Marco Otte-Witte (bug fix)
|
|
22
|
-
define_method :extend do |*
|
|
23
|
-
extend_method.
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
define_method :extend do |*mods, &blk|
|
|
37
|
+
return extend_method.bind_call(self, *mods) unless blk
|
|
38
|
+
warn "Kernel#extend with a block is deprecated. Use obj.extend(Module.new { ... }) instead. " \
|
|
39
|
+
"It will be removed after 2027-09-30.", uplevel: 1
|
|
40
|
+
extend_method.bind_call(self, *mods) unless mods.empty?
|
|
41
|
+
extend_method.bind_call(self, Module.new(&blk))
|
|
26
42
|
end
|
|
27
43
|
|
|
28
44
|
end
|
|
@@ -4,7 +4,7 @@ module Kernel
|
|
|
4
4
|
|
|
5
5
|
# Returns a Functor that intercepts method calls, forwards
|
|
6
6
|
# them to self for side effects, and returns self. This is like
|
|
7
|
-
# a block-less version of #tap that allows method chaining
|
|
7
|
+
# a block-less version of #tap that allows method chaining:
|
|
8
8
|
#
|
|
9
9
|
# YAML.functor.load_file('foo.yml').load_file('bar.yml')
|
|
10
10
|
#
|
|
@@ -6,9 +6,8 @@ module Kernel
|
|
|
6
6
|
# @a #=> 1
|
|
7
7
|
# @b #=> 2
|
|
8
8
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
# the better choice until such time that Ruby supports anonymous delegators.
|
|
9
|
+
# `Mirror#update`, from `require 'facets/mirror'`, does the same
|
|
10
|
+
# through a mirror: `obj.mirror.update(a: 1)`.
|
|
12
11
|
#
|
|
13
12
|
def instance_assign(hash)
|
|
14
13
|
hash.each do |k,v|
|