facets 4.0.0 → 4.1.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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +18 -2
  3. data/HISTORY.md +205 -0
  4. data/README.md +8 -2
  5. data/lib/core/facets/array/split.rb +16 -9
  6. data/lib/core/facets/class/descendants.rb +21 -8
  7. data/lib/core/facets/enumerable/defer.rb +4 -1
  8. data/lib/core/facets/enumerable/graph.rb +1 -1
  9. data/lib/core/facets/enumerable/map_by.rb +6 -2
  10. data/lib/core/facets/hash/count.rb +19 -8
  11. data/lib/core/facets/hash/deep_merge.rb +25 -20
  12. data/lib/core/facets/integer/ordinal.rb +18 -13
  13. data/lib/core/facets/kernel/blank.rb +9 -4
  14. data/lib/core/facets/kernel/deep_clone.rb +36 -8
  15. data/lib/core/facets/kernel/extend.rb +20 -4
  16. data/lib/core/facets/module/alias_class_method.rb +8 -3
  17. data/lib/core/facets/module/module_def.rb +0 -11
  18. data/lib/core/facets/module/redefine_method.rb +12 -3
  19. data/lib/core/facets/range/overlap.rb +32 -1
  20. data/lib/core/facets/string/indent.rb +32 -20
  21. data/lib/core/facets/string/remove.rb +13 -8
  22. data/lib/core/facets/string/squish.rb +5 -3
  23. data/lib/core/facets/string.rb +1 -0
  24. data/lib/core/facets/time/change.rb +50 -28
  25. data/lib/core/facets/time/future.rb +6 -2
  26. data/lib/core/facets/time/round_to.rb +1 -1
  27. data/lib/core/facets/time/to_time.rb +3 -7
  28. data/lib/core/facets/version.rb +1 -1
  29. data/lib/standard/facets/cloneable.rb +4 -3
  30. data/lib/standard/facets/digest/salted_digest.rb +6 -2
  31. data/lib/standard/facets/fileutils/ln_r.rb +7 -56
  32. data/lib/standard/facets/find/select.rb +3 -1
  33. data/lib/standard/facets/interval.rb +1 -1
  34. data/lib/standard/facets/math/percentile.rb +9 -4
  35. data/lib/standard/facets/ostruct/initialize.rb +7 -6
  36. data/lib/standard/facets/ostruct.rb +3 -3
  37. data/lib/standard/facets/pathname/glob.rb +11 -7
  38. data/lib/standard/facets/shellwords.rb +59 -15
  39. data/lib/standard/facets/zlib.rb +5 -9
  40. metadata +1 -1
@@ -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 |*mod, &blk|
23
- extend_method.bind(self).call(*mod) unless mod.empty?
24
- extend_method.bind(self).call(Module.new(&blk)) if blk
25
- self
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
@@ -2,10 +2,15 @@ class Module
2
2
 
3
3
  # Alias a class method.
4
4
  #
5
+ # class Greeter
6
+ # def self.hello; "Hello"; end
7
+ # alias_class_method :hi, :hello
8
+ # end
9
+ #
10
+ # Greeter.hi #=> "Hello"
11
+ #
5
12
  def alias_class_method(name, target)
6
- class << self
7
- alias_method(name, target)
8
- end
13
+ singleton_class.alias_method(name, target)
9
14
  end
10
15
 
11
16
  end
@@ -16,16 +16,5 @@ class Module
16
16
  module_eval { define_method name, &blk }
17
17
  end
18
18
 
19
- #--
20
- # TODO: Why are we making these protected,
21
- # and where should these really go in Facets?
22
- #++
23
- protected :attr
24
- protected :attr_reader
25
- protected :attr_writer
26
- protected :attr_accessor
27
- protected :remove_method
28
- protected :undef_method
29
-
30
19
  end
31
20
 
@@ -1,7 +1,5 @@
1
1
  class Module
2
2
 
3
- private
4
-
5
3
  # Creates a new method for a pre-existing method.
6
4
  #
7
5
  # If _aka_ is given, then the method being redefined will
@@ -21,14 +19,25 @@ class Module
21
19
  #
22
20
  # Greeter.new.hello #=> "Hello, friend!"
23
21
  #
22
+ # The new method keeps the visibility of the one it replaces. Like
23
+ # ActiveSupport's Module#redefine_method, this is public.
24
+ #
24
25
  # CREDIT: Trans
25
26
 
26
27
  def redefine_method(sym, aka=nil, &blk)
28
+ visibility = if private_method_defined?(sym)
29
+ :private
30
+ elsif protected_method_defined?(sym)
31
+ :protected
32
+ else
33
+ :public
34
+ end
27
35
  alias_method(aka, sym) if aka
28
36
  ## prevent warning about method overwrite
29
37
  begin remove_method(sym) rescue NameError end
30
- ## define replacelemt method
38
+ ## define replacement method
31
39
  define_method(sym, &blk)
40
+ __send__(visibility, sym)
32
41
  end
33
42
 
34
43
  #
@@ -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
- include?(other.first) or other.include?(first)
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
 
@@ -1,9 +1,28 @@
1
1
  class String
2
2
 
3
- # Indent left or right by `n` spaces, or `n` number of `c` string.
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 - The number of spaces to indent. [Integer]
6
- # c - Character to use, if other then space. [String]
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(/^/, c * n)
36
+ gsub(empty_lines ? /^/ : /^(?!$)/, c * n)
17
37
  else
18
- gsub(/^#{Regexp.escape(c)}{0,#{-n}}/, "")
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
@@ -52,15 +69,10 @@ class String
52
69
  if size
53
70
  indent(-size)
54
71
  else
55
- char = ' '
56
- self.scan(/^[\ \t]*\S/) do |m|
57
- if size.nil? || m.size < size
58
- size = m.size
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
 
@@ -1,17 +1,22 @@
1
1
  class String
2
2
 
3
- # Removes all occurrences of a pattern in a string.
3
+ # Removes all occurrences of one or more patterns in a string.
4
4
  #
5
- # Returns a new [String] with all occurrences of the pattern removed.
6
- def remove(pattern)
7
- gsub(pattern, '')
5
+ # "foo bar".remove("o") #=> "f bar"
6
+ # "foo bar".remove(/o/, "b") #=> "f ar"
7
+ #
8
+ # Returns a new [String] with all occurrences of the patterns removed.
9
+ def remove(*patterns)
10
+ dup.remove!(*patterns)
8
11
  end
9
12
 
10
- # Removes all occurrences of a pattern in a string.
13
+ # Removes all occurrences of one or more patterns in a string.
11
14
  #
12
- # Returns the [String] with all occurrences of the pattern removed.
13
- def remove!(pattern)
14
- gsub!(pattern, '')
15
+ # Returns the [String] with all occurrences of the patterns removed,
16
+ # even if nothing was removed.
17
+ def remove!(*patterns)
18
+ patterns.each { |pattern| gsub!(pattern, '') }
19
+ self
15
20
  end
16
21
 
17
22
  # Removes occurances of a string or regexp. This is an operator
@@ -1,7 +1,9 @@
1
1
  class String
2
+
2
3
  # Returns the string, first removing all whitespace on both ends of
3
4
  # the string, and then changing remaining consecutive whitespace
4
- # groups into one space each.
5
+ # groups into one space each. Unicode whitespace, such as a
6
+ # non-breaking space, counts too, as it does in ActiveSupport.
5
7
  #
6
8
  # %{ Multi-line
7
9
  # string }.squish # => "Multi-line string"
@@ -14,9 +16,9 @@ class String
14
16
 
15
17
  # Performs a destructive squish. See String#squish.
16
18
  def squish!
19
+ gsub!(/[[:space:]]+/, ' ')
17
20
  strip!
18
- gsub!(/\s+/, ' ')
19
21
  self
20
22
  end
21
- end
22
23
 
24
+ end
@@ -54,6 +54,7 @@ require_relative 'string/store.rb'
54
54
  require_relative 'string/subtract.rb'
55
55
  require_relative 'string/titlecase.rb'
56
56
  require_relative 'string/to_re.rb'
57
+ require_relative 'string/trim.rb'
57
58
  require_relative 'string/unbracket.rb'
58
59
  require_relative 'string/underscore.rb'
59
60
  require_relative 'string/unfold.rb'
@@ -2,7 +2,7 @@ class Time
2
2
 
3
3
  # Returns a new Time where one or more of the elements
4
4
  # have been changed according to the +options+ parameter.
5
- # The time options (hour, minute, sec, usec) reset
5
+ # The time options (hour, min, sec, usec) reset
6
6
  # cascadingly, so if only the hour is passed, then
7
7
  # minute, sec, and usec is set to 0. If the hour and
8
8
  # minute is passed, then sec and usec is set to 0.
@@ -13,34 +13,56 @@ class Time
13
13
  # t2 = t1.change(:hour => 11)
14
14
  # t2.ctime #=> "Wed Dec 31 11:00:00 1969"
15
15
  #
16
+ # The options are :year, :month, :day, :hour, :min, :sec, :usec, :nsec
17
+ # and :offset. The new time keeps this time's UTC offset or time zone,
18
+ # unless :offset gives a new one.
19
+ #
20
+ # t = Time.new(2026, 11, 4, 15, 30, 0, "+09:00")
21
+ # t.change(hour: 1) #=> 2026-11-04 01:00:00 +0900
22
+ # t.change(offset: "+01:00") #=> 2026-11-04 15:30:00 +0100
23
+ #
24
+ # It takes the same options and gives the same results as ActiveSupport's
25
+ # Time#change, so the two agree whichever is loaded.
26
+ #
16
27
  def change(options)
17
- opts=options; #{}; options.each_pair{ |k,v| opts[k] = v.to_i }
18
- self.class.send(
19
- self.utc? ? :utc : :local,
20
- opts[:year] || self.year,
21
- opts[:month] || self.month,
22
- opts[:day] || self.day,
23
- opts[:hour] || self.hour,
24
- opts[:min] || (opts[:hour] ? 0 : self.min),
25
- opts[:sec] || ((opts[:hour] || opts[:min]) ? 0 : self.sec),
26
- opts[:usec] || ((opts[:hour] || opts[:min] || opts[:sec]) ? 0 : self.usec)
27
- )
28
- end
28
+ year = options.fetch(:year, self.year)
29
+ month = options.fetch(:month, self.month)
30
+ day = options.fetch(:day, self.day)
31
+ hour = options.fetch(:hour, self.hour)
32
+ min = options.fetch(:min, options[:hour] ? 0 : self.min)
33
+ sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : self.sec)
29
34
 
30
- # Old Version ...
31
- #
32
- # def change(options)
33
- # ::Time.send(
34
- # self.utc? ? :utc_time : :local_time,
35
- # options[:year] || self.year,
36
- # options[:month] || self.month,
37
- # options[:day] || self.day,
38
- # options[:hour] || self.hour,
39
- # options[:min] || (options[:hour] ? 0 : self.min),
40
- # options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
41
- # options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
42
- # )
43
- # end
35
+ if options[:nsec]
36
+ raise ArgumentError, "can't change both :nsec and :usec: #{options.inspect}" if options[:usec]
37
+ usec = Rational(options[:nsec], 1000)
38
+ else
39
+ reset = options[:hour] || options[:min] || options[:sec]
40
+ usec = options.fetch(:usec, reset ? 0 : Rational(nsec, 1000))
41
+ end
42
+ raise ArgumentError, "argument out of range" if usec >= 1_000_000
43
+ sec += Rational(usec, 1_000_000)
44
44
 
45
- end
45
+ if options[:offset]
46
+ self.class.new(year, month, day, hour, min, sec, options[:offset])
47
+ elsif utc?
48
+ self.class.utc(year, month, day, hour, min, sec)
49
+ elsif zone.respond_to?(:utc_to_local)
50
+ # A time zone object, e.g. from TZInfo.
51
+ time = self.class.new(year, month, day, hour, min, sec, zone)
52
+ time += 0 unless time.utc_offset.integer? # a Ruby bug before 3.2.4 and 3.3.1
53
+ # In the hour that repeats when daylight saving time ends, Time.new
54
+ # picks the earlier of the two times. Pick the one with this time's
55
+ # offset instead, if that's the later one.
56
+ shift = time.utc_offset - utc_offset
57
+ later = time + shift if shift > 0
58
+ later && later.utc_offset == utc_offset ? later : time
59
+ elsif zone
60
+ # Local time. isdst picks the right time in the hour that repeats.
61
+ self.class.local(sec, min, hour, day, month, year, nil, nil, isdst, nil)
62
+ else
63
+ # A fixed UTC offset, e.g. +09:00.
64
+ self.class.new(year, month, day, hour, min, sec, utc_offset)
65
+ end
66
+ end
46
67
 
68
+ end
@@ -1,16 +1,20 @@
1
1
  class Time
2
2
 
3
+ # The current time. That's Time.now, unless ActiveSupport's Time.zone is
4
+ # set, in which case it's the current time in that zone, as in
5
+ # ActiveSupport's own Time.current. So the two agree whichever is loaded.
3
6
  def self.current
4
- now
7
+ respond_to?(:zone) && zone ? zone.now : now
5
8
  end
6
9
 
10
+ # Is this time before +other+, or before now if not given?
7
11
  def past?(other=nil)
8
12
  self < (other || self.class.current)
9
13
  end
10
14
 
15
+ # Is this time after +other+, or after now if not given?
11
16
  def future?(other=nil)
12
17
  self > (other || self.class.current)
13
18
  end
14
19
 
15
20
  end
16
-
@@ -14,7 +14,7 @@ class Time
14
14
  # TODO: Fractional seconds should round the usec.
15
15
  #
16
16
  def round_to(seconds)
17
- (self + seconds / 2.0).trunc(seconds)
17
+ (self + seconds / 2.0).floor_to(seconds)
18
18
  end
19
19
 
20
20
  end
@@ -8,18 +8,14 @@ class Time
8
8
  # @deprecated Ruby defines Time#to_time once 'date' is loaded.
9
9
  # Scheduled for removal after 2027-09-30.
10
10
  #
11
+ # Like Ruby's own, it returns +self+, keeping the time's UTC offset.
12
+ #
11
13
  def to_time
12
14
  warn "Facets' Time#to_time is deprecated. Require 'date' for Ruby's own Time#to_time. " \
13
15
  "It will be removed after 2027-09-30.", uplevel: 1
14
- getlocal
16
+ self
15
17
  end
16
18
 
17
- # A method to keep Time, Date and DateTime instances interchangeable
18
- # on conversions. In this case, it simply returns +self+.
19
- #def to_time
20
- # self
21
- #end
22
-
23
19
  end
24
20
 
25
21
  end
@@ -1,3 +1,3 @@
1
1
  module Facets
2
- VERSION = '4.0.0'
2
+ VERSION = '4.1.0'
3
3
  end
@@ -1,7 +1,8 @@
1
- # @deprecated Use deep_dup/deep_clone instead, or define your own
2
- # initialize_copy method.
1
+ # @deprecated Use Kernel#deep_clone (`require 'facets/kernel/deep_clone'`)
2
+ # instead, or define your own initialize_copy method.
3
3
  # Scheduled for removal after 2027-09-30.
4
- warn "facets/cloneable is deprecated. Use deep_dup/deep_clone instead. It will be removed after 2027-09-30.", uplevel: 1
4
+ warn "facets/cloneable is deprecated. Use Kernel#deep_clone (require 'facets/kernel/deep_clone') " \
5
+ "or define your own initialize_copy instead. It will be removed after 2027-09-30.", uplevel: 1
5
6
 
6
7
  module Cloneable
7
8
 
@@ -1,13 +1,17 @@
1
1
  require 'digest'
2
- require 'facets/string/random_binary'
2
+ require 'securerandom'
3
3
 
4
4
  module Digest
5
5
 
6
6
  module Instance
7
+ # The digest of +str+ followed by +salt+, with the salt appended.
8
+ # With +salt+ :auto, the salt is +digest_length+ random bytes from
9
+ # SecureRandom.
10
+ #
7
11
  # CREDIT: Guido De Rosa
8
12
  def salted_digest(str='', salt=:auto)
9
13
  if salt == :auto
10
- salt = String.random_binary(digest_length)
14
+ salt = SecureRandom.random_bytes(digest_length)
11
15
  end
12
16
  digest(str + salt) + salt
13
17
  end
@@ -1,6 +1,8 @@
1
+ require 'fileutils'
2
+
1
3
  module FileUtils
2
4
 
3
- unless method_defined?(:ln_r)
5
+ unless respond_to?(:ln_r)
4
6
 
5
7
  #
6
8
  # Options: noop verbose dereference_root remove_destination
@@ -25,6 +27,9 @@ module FileUtils
25
27
  # FileUtils.ln_r 'src/.', 'dest' # cp_r('src', 'dest') makes src/dest,
26
28
  # # but this doesn't.
27
29
  #
30
+ # It is much like Ruby's own FileUtils.cp_lr, and uses Ruby's
31
+ # FileUtils.link_entry to make the links.
32
+ #
28
33
  # TODO: Why --remove-destination and not just --force?
29
34
  def ln_r(src, dest, options = {})
30
35
  fu_output_message "ln -r#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
@@ -37,61 +42,7 @@ module FileUtils
37
42
  end
38
43
  module_function :ln_r
39
44
 
40
- OPT_TABLE = {} unless const_defined?(:OPT_TABLE)
41
- OPT_TABLE['ln_r'] = [:noop, :verbose, :dereference_root, :remove_destination]
42
-
43
- #
44
- # Hard links a file system entry +src+ to +dest+.
45
- # If +src+ is a directory, this method links its contents recursively.
46
- #
47
- # Both of +src+ and +dest+ must be a path name.
48
- # +src+ must exist, +dest+ must not exist.
49
- #
50
- # If +dereference_root+ is true, this method dereference tree root.
51
- #
52
- # If +remove_destination+ is true, this method removes each destination file before copy.
53
- #
54
- def link_entry(src, dest, dereference_root = false, remove_destination = false)
55
- Entry_.new(src, nil, dereference_root).traverse do |ent|
56
- destent = Entry_.new(dest, ent.rel, false)
57
- File.unlink destent.path if remove_destination && File.file?(destent.path)
58
- ent.link destent.path
59
- end
60
- end
61
- module_function :link_entry
62
-
63
- class Entry_ #:nodoc:
64
- def link(dest)
65
- case
66
- when directory?
67
- if !File.exist?(dest) and descendant_diretory?(dest, path)
68
- raise ArgumentError, "cannot link directory %s to itself %s" % [path, dest]
69
- end
70
- begin
71
- Dir.mkdir dest
72
- rescue
73
- raise unless File.directory?(dest)
74
- end
75
- else file?
76
- File.link path(), dest
77
- end
78
- end
79
-
80
- unless const_defined?(:DIRECTORY_TERM)
81
- if File::ALT_SEPARATOR
82
- DIRECTORY_TERM = "(?=[/#{Regexp.quote(File::ALT_SEPARATOR)}]|\\z)".freeze
83
- else
84
- DIRECTORY_TERM = "(?=/|\\z)".freeze
85
- end
86
- SYSCASE = File::FNM_SYSCASE.nonzero? ? "-i" : ""
87
- end
88
-
89
- unless method_defined?(:descendant_diretory?)
90
- def descendant_diretory?(descendant, ascendant)
91
- /\A(?#{SYSCASE}:#{Regexp.quote(ascendant)})#{DIRECTORY_TERM}/ =~ File.dirname(descendant)
92
- end
93
- end
94
- end
45
+ OPT_TABLE['ln_r'] = [:noop, :verbose, :dereference_root, :remove_destination] if const_defined?(:OPT_TABLE) && !OPT_TABLE.frozen?
95
46
 
96
47
  end
97
48
 
@@ -1,3 +1,5 @@
1
+ require 'find'
2
+
1
3
  module Find
2
4
  # Identical to find except select returns the matching files as an array.
3
5
  # (find returns nil, which is not very useful if you actually wanted an array.)
@@ -8,7 +10,7 @@ module Find
8
10
  #
9
11
  # CREDIT: Tyler Rick
10
12
  def self.select(*paths, &block)
11
- Enumerator.new(self, :find, *paths).select{|value| yield value}
13
+ find(*paths).select(&block)
12
14
  end
13
15
  end
14
16
 
@@ -257,7 +257,7 @@ class Interval
257
257
  end
258
258
  end
259
259
  else
260
- Enumerator.new(self, :step, n, d)
260
+ to_enum(:step, n, d)
261
261
  end
262
262
  end
263
263
 
@@ -16,19 +16,24 @@ module Math
16
16
  # require 'facets/math/percentile'
17
17
  #
18
18
  def self.percentile(array, pcnt)
19
- sorted_array = array.sort
20
-
21
19
  return nil if array.length == 0
22
20
 
21
+ sorted_array = array.sort
22
+
23
23
  rank = (pcnt.to_f / 100) * (array.length + 1)
24
24
  whole = rank.truncate
25
-
25
+
26
+ # A rank below 1 or past the last value is clamped to the minimum or
27
+ # maximum, as NIST specifies.
28
+ return sorted_array.first if whole < 1
29
+ return sorted_array.last if whole >= array.length
30
+
26
31
  # if has fractional part
27
32
  if whole != rank
28
33
  s0 = sorted_array[whole - 1]
29
34
  s1 = sorted_array[whole]
30
35
 
31
- f = (rank - rank.truncate).abs
36
+ f = rank - whole
32
37
 
33
38
  return (f * (s1 - s0)) + s0
34
39
  else
@@ -2,6 +2,9 @@ require 'ostruct'
2
2
 
3
3
  class OpenStruct
4
4
 
5
+ alias_method :__initialize_without_block__, :initialize
6
+ private :__initialize_without_block__
7
+
5
8
  # Allows the initialization of an OpenStruct with a setter block:
6
9
  #
7
10
  # person = OpenStruct.new do |o|
@@ -17,14 +20,12 @@ class OpenStruct
17
20
  # p.gender = :M
18
21
  # end
19
22
  #
23
+ # The hash is handled by Ruby's own OpenStruct#initialize, which this
24
+ # calls first.
25
+ #
20
26
  # CREDIT: Noah Gibbs, Gavin Sinclair
21
27
  def initialize(hash=nil, &block)
22
- @table = {}
23
- if hash
24
- hash.each_pair do |k, v|
25
- self[k.to_sym] = v
26
- end
27
- end
28
+ __initialize_without_block__(hash)
28
29
  if block && block.arity == 1
29
30
  yield self
30
31
  end