y_support 2.0.42 → 2.0.43

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7a8d4d02757ef3f1d47a1093c04274fd462d890
4
- data.tar.gz: 66d1c435eb4091ce1b749899af392aaa718c241a
3
+ metadata.gz: a7f4a0b0a8c590f42dd23407f86cccce3ea0b95b
4
+ data.tar.gz: 2cb43420440e01d7932a373aa197204f3b7115d3
5
5
  SHA512:
6
- metadata.gz: 57036903f19cab0194e726943b98dec0d2becb6808ac26f1593097949e6e9c49aadc0a2a3149e0737b8dc03c88134512953d980ac89a53dd18df74c5f1e8836e
7
- data.tar.gz: 63801b791d327a8f3b9f894c8659be66f8f4bc10e7376ebffb6e1d5666314106d803ad5ed384fe0574ffd40530422c960e1c1e9df1730d88755dab94e6a1892e
6
+ metadata.gz: 13398ec84e28cd5fdda66ccd6b040f403129a74ddc88cb3b91a264117e08a0469cba1e0006cc5d03c541e697fb17b60c41248fb711b26a9ab0a240a21c08467b
7
+ data.tar.gz: 5e3031f03fd0de7d043ace4d4b7e5c7a1f1a04ae042f6869c14bfa1e67776035efe804dd9ade11c999f441bf10c2f1a6856ca8b610f44509d4fcc257c3238a98
data/README.md CHANGED
@@ -5,7 +5,8 @@
5
5
  helpful methods, which can be divided as follows
6
6
 
7
7
  * `NameMagic` (`lib/name_magic.rb`) -- its main feature is that it allows
8
- constant magic known from classes to work with any objects.
8
+ constant magic so well known from Ruby classes (`Foo = Class.new;
9
+ Foo.name #=> "Foo"`) to work with any objects.
9
10
  * Miscellaneous helpful methods (`lib/misc.rb`)
10
11
  * Typing (runtime assertions, `lib/typing.rb`)
11
12
  * Other smaller components:
@@ -16,7 +17,17 @@ helpful methods, which can be divided as follows
16
17
 
17
18
  ## NameMagic
18
19
 
19
- Try for example:
20
+ Ruby classes are well known for their "constant magic": When they are assigned
21
+ to constants, they acquire `name` attribute corresponding to the constant name.
22
+ ```ruby
23
+ x = Class.new
24
+ x.name #=> nil
25
+ Foo = x
26
+ x.name #=> "Foo"
27
+ ```
28
+ NameMagic mixin lends this feature to any Ruby class. The class in which
29
+ NameMagic is mixed in will also hold the registry of the instances, named or
30
+ unnamed. Example code:
20
31
  ```ruby
21
32
  require 'y_support/name_magic'
22
33
 
@@ -30,16 +41,20 @@ Try for example:
30
41
  class Cat < Animal; def sound; "meow" end end
31
42
 
32
43
  Pochi = Dog.new
33
- unnamed_kitten = Cat.new
44
+ anonymous_kitten = Cat.new
34
45
  ```
35
46
  Mixin `NameMagic` makes class `Animal` keep registry of its instances:
36
47
  ```ruby
37
48
  Animal.instances.names
38
49
  #=> [:Pochi, nil]
39
- Tama = unnamed_kitten
50
+ Tama = anonymous_kitten
40
51
  Animal.instances.names
41
52
  #=> [:Pochi, :Tama]
42
- Cheburashka = Animal.new
53
+ # Name can also be supplied to the constructor explicitly:
54
+ Animal.new name: :Cheburashka
55
+ Animal.instance( "Cheburashka" )
56
+ Animal.instance( "Cheburashka" ).name
57
+ :Cheburashka
43
58
  Animal.instances.names
44
59
  #=> [:Pochi, :Tama, :Cheburashka]
45
60
  Dog.instances.names
@@ -48,6 +63,14 @@ Mixin `NameMagic` makes class `Animal` keep registry of its instances:
48
63
  Dog.instances.each &:speak
49
64
  Cat.instances.each &:speak
50
65
  ```
66
+ The registry of instances is maintained by the namespace, so the instances can
67
+ only be garbage collected after the namespace is garbage collected. If the user
68
+ wants to enable this earlier, the namespace can be ordered to forget them:
69
+ ```ruby
70
+ Animal.forget_all_instances
71
+ Animal.instances
72
+ []
73
+ ```
51
74
 
52
75
  ## Other components
53
76
 
@@ -3,20 +3,24 @@
3
3
  require 'matrix'
4
4
 
5
5
  class Array
6
- # This would collide with built-in #to_hash method.
6
+ # Converts an array, whose elements are also arrays, to a hash. Head
7
+ # (position 0) of each array is made to point at the rest of the array
8
+ # (tail), normally starting immediately after the head (position 1). The
9
+ # starting position of the tail can be controlled by an optional
10
+ # argument. Tails of 2 and more elements are represented as arrays.
7
11
  #
8
- # # Converts an array, whose elements are also arrays, to a hash. Head
9
- # # (position 0) of each array is made to point at the rest of the array
10
- # # (tail), normally starting immediately after the head (position 1). The
11
- # # starting position of the tail can be controlled by an optional
12
- # # argument. Tails of 2 and more elements are represented as arrays.
13
- # #
14
- # def to_hash( tail_from = 1 )
15
- # self.reject { | e | e[0].nil? }.reduce({}) { |a, e|
16
- # tail = e[tail_from..-1]
17
- # a.merge( { e[0] => tail.size >= 2 ? tail : tail[0] } )
18
- # }
19
- # end
12
+ def arrays_to_hash( tail_from = 1 )
13
+ no_nil_heads = begin
14
+ self.reject { | e | e[0].nil? }
15
+ rescue NoMethodError => err
16
+ raise TypeError, "The receiver must be an array of " +
17
+ "arrays! (#{err})"
18
+ end
19
+ no_nil_heads.each_with_object Hash.new do |element, memo|
20
+ tail = element[ tail_from .. -1 ]
21
+ memo.update element.first => ( tail.size > 1 ? tail : tail.first )
22
+ end
23
+ end
20
24
 
21
25
  # Zips this array with another collection into a hash. If a block is given,
22
26
  # it is applied to each element of the array to get the hash values.
@@ -39,6 +43,37 @@ class Array
39
43
  zip_to_hash collection
40
44
  end
41
45
 
46
+ # Assuming an array of comparable elements ordered in an ascending order,
47
+ # this method expects an argument comparable with the elements, and returns
48
+ # the nearest smaller or equal element. The second optional ordered argument,
49
+ # true by default, controls whether equality is OK. If set to false, then
50
+ # the nearest _smaller_ element is sought.
51
+ #
52
+ def ascending_floor arg, accept_equal=true
53
+ idx = if accept_equal then
54
+ find_index { |e| e > arg }
55
+ else find_index { |e| e >= arg } end
56
+ case idx
57
+ when 0 then nil
58
+ when nil then last
59
+ else fetch idx - 1 end
60
+ end
61
+
62
+ # Assuming an array of comparable elements ordered in an ascending order,
63
+ # this method expects an argument comparable with the elements, and returns
64
+ # the neares greater or equal element. The second optional ordered argument,
65
+ # true by default, controls whether equality is OK. If set to false, then the
66
+ # nearest _greater_ element is sought.
67
+ #
68
+ def ascending_ceiling arg, accept_equal=true
69
+ idx = if accept_equal then
70
+ find_index { |e| not e < arg }
71
+ else find_index { |e| not e <= arg } end
72
+ case idx
73
+ when nil then nil
74
+ else fetch idx end
75
+ end
76
+
42
77
  # Allows style &[ function, *arguments ]
43
78
  #
44
79
  def to_proc
@@ -104,8 +139,6 @@ class Array
104
139
  other.map { |e| index e }
105
140
  end
106
141
 
107
- # TEST ME ** FUCK OFF TESTS
108
-
109
142
  # Pretty-prints the array as line, with prescribed +:precision+ and +:gap+
110
143
  # (both are named arguments).
111
144
  #
@@ -1,4 +1,4 @@
1
1
  module YSupport
2
- VERSION = "2.0.42"
2
+ VERSION = "2.0.43"
3
3
  DEBUG = false
4
4
  end
data/test/misc_test.rb CHANGED
@@ -76,6 +76,13 @@ describe Array do
76
76
  require 'y_support/core_ext/array'
77
77
  end
78
78
 
79
+ it "has #arrays_to_hash" do
80
+ [ [ :a, 1 ], [ :b, 2 ] ].arrays_to_hash
81
+ .must_equal( { a: 1, b: 2 } )
82
+ [ [ :a, 1, 2 ], [ :b, 2, 3 ] ].arrays_to_hash
83
+ .must_equal( { a: [ 1, 2 ], b: [ 2, 3 ] } )
84
+ end
85
+
79
86
  it "has #zip_to_hash" do
80
87
  assert_equal( {a: 1, b: 2}, [:a, :b].zip_to_hash( [1, 2] ) )
81
88
  assert_equal( {a: "a"}, [:a].zip_to_hash( &:to_s ) )
@@ -85,6 +92,26 @@ describe Array do
85
92
  assert_equal( {a: 1, b: 2}, [:a, :b] >> [1, 2] )
86
93
  end
87
94
 
95
+ it "has #ascending_floor" do
96
+ a = 1, 2, 3
97
+ a.ascending_floor( 0.5 ).must_equal nil
98
+ a.ascending_floor( 1 ).must_equal 1
99
+ a.ascending_floor( 1.5 ).must_equal 1
100
+ a.ascending_floor( 3.5 ).must_equal 3
101
+ a.ascending_floor( 1, false ).must_equal nil
102
+ a.ascending_floor( 3, false ).must_equal 2
103
+ end
104
+
105
+ it "has #ascending_ceiling" do
106
+ a = 1, 2, 3
107
+ a.ascending_ceiling( 0.5 ).must_equal 1
108
+ a.ascending_ceiling( 1.5 ).must_equal 2
109
+ a.ascending_ceiling( 3 ).must_equal 3
110
+ a.ascending_ceiling( 3.1 ).must_equal nil
111
+ a.ascending_ceiling( 3, false ).must_equal nil
112
+ a.ascending_ceiling( 2, false ).must_equal 3
113
+ end
114
+
88
115
  it "has #to_proc in style &[function, *args]" do
89
116
  assert_equal [2, 3], [1, 2].map( &[:+, 1] )
90
117
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: y_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.42
4
+ version: 2.0.43
5
5
  platform: ruby
6
6
  authors:
7
7
  - boris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-18 00:00:00.000000000 Z
11
+ date: 2013-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport