y_support 2.0.21 → 2.0.23

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: 6981d4eb5fdec5cb5eaf09321d4081c39b65c4db
4
- data.tar.gz: 472b3008961422d3823f0b5b654ca2ecfadbd4f1
3
+ metadata.gz: 0143322533b38e137de87f30d4d5d94021a0e211
4
+ data.tar.gz: f086f7e148dbd589c5f4007e689c4519c09c80b9
5
5
  SHA512:
6
- metadata.gz: c0fe48e25cd93a0b8abce16b12b227d408f1f2e99b5f0275abd13a56735ef78e7b303ad22f18fa9ff15e695f02cd050e3ae32cd3d2130786e9c479ffacc2549f
7
- data.tar.gz: dbcfa5713ba17cbc1dc64e65f18ad31497faeb82d6943cead57f1504b9e78e1c3ef17bf61d9b18a1684ca8be7e0f224c21b935c16884e3abe2cb0fcc726265a6
6
+ metadata.gz: 602b5759bd46e3138145086c168cb695a0433f6efa3d114d4437186eec2b16507167326bbbb3247d058d1e69f0deae3b8d1e16a3b1d60bd4fbe127bd24ff66fc
7
+ data.tar.gz: 6473a5334642f9d423495aa02e925532dc7e1a44074d4a4b8d41760756084bb0401686a75b1a371f577927e655dcf2dc4758647111321136eebf368e0f7db5e3
@@ -209,7 +209,7 @@ class Matrix
209
209
  end
210
210
  end
211
211
  return new_matrix( rows, arg.column_size )
212
- when SY::Magnitude # newly added - multiplication by a magnitude
212
+ when ( SY::Magnitude rescue :SY_Magnitude_absent ) # newly added - multiplication by a magnitude
213
213
  # I am not happy with this explicit switch on SY::Magnitude type here.
214
214
  # Perhaps coerce should handle this?
215
215
  rows = Array.new row_size do |i|
@@ -86,7 +86,20 @@ class Array
86
86
  def to_column_vector
87
87
  Matrix.column_vector self
88
88
  end
89
+
90
+ # Returns correspondence matrix to another array.
91
+ #
92
+ def correspondence_matrix other
93
+ Matrix.correspondence_matrix self, other
94
+ end
89
95
 
96
+ # Returns indices of elements of another array in this array (inverse of
97
+ # #values_at).
98
+ #
99
+ def indices_of other
100
+ other.map { |e| index e }
101
+ end
102
+
90
103
  # TEST ME
91
104
  # def pretty_inspect
92
105
  # each_slice( 4 ) { |slice|
@@ -0,0 +1,16 @@
1
+ #encoding: utf-8
2
+
3
+ class Class
4
+ # Creates a subclass of the current class parametrized with a given set of
5
+ # parameters. The parameters have form { symbol: value } and they cause
6
+ # singleton method(s) named "symbol" be defined on the subclass, returning
7
+ # "value".
8
+ #
9
+ def parametrize **parameters
10
+ Class.new( self ).tap do |subclass|
11
+ parameters.each_pair { |symbol, value|
12
+ subclass.define_singleton_method symbol do value end
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ require 'y_support'
2
+ require 'y_support/core_ext/class/misc'
@@ -6,48 +6,46 @@ class Hash
6
6
  # reversed merge!: defaults.merge( self! )
7
7
  alias :default! :reverse_merge!
8
8
 
9
- # Applies a block as a mapping on all keys, returning a new hash
9
+ # Applies a block as a mapping on all keys, returning a new hash.
10
+ #
10
11
  def with_keys
11
- keys.each_with_object self.class.new do |hash_key, ꜧ|
12
- [ yield( hash_key ) ] = self[ hash_key ]
12
+ keys.each_with_object self.class.new do |hash_key, hsh|
13
+ hsh[ yield hash_key ] = self[ hash_key ]
13
14
  end
14
15
  end
15
- alias :do_with_keys :with_keys
16
16
 
17
- # The difference from do_with_keys is that modify_keys expects block
18
- # that takes 2 arguments (key: value pair) and returns the new key.
17
+ # The difference from #with_keys is that modify_keys expects block that takes
18
+ # 2 arguments (key: value pair) and returns the new key.
19
+ #
19
20
  def modify_keys
20
- each_with_object self.class.new do |hash_pair, ꜧ|
21
- [ yield( hash_pair ) ] = self[ hash_pair[0] ]
21
+ each_with_object self.class.new do |hash_pair, hsh|
22
+ hsh[ yield( hash_pair ) ] = self[ hash_pair[0] ]
22
23
  end
23
24
  end
24
25
 
25
- # Applies a block as a mapping on all values, returning a new hash
26
+ # Applies a block as a mapping on all values, returning a new hash.
27
+ #
26
28
  def with_values
27
- each_with_object self.class.new do |hash_pair, ꜧ|
28
- ꜧ[ hash_pair[0] ] = yield( hash_pair[1] )
29
- end
29
+ each_with_object self.class.new do |(k, v), hsh| hsh[ k ] = yield v end
30
30
  end
31
- alias :do_with_values :with_values
32
31
 
33
32
  # Like #do_with_values, but modifies the receiver.
33
+ #
34
34
  def with_values!
35
- each_with_object self do |hash_pair, ꜧ|
36
- hash_key, hash_val = hash_pair
37
- ꜧ[ hash_key ] = yield( hash_val )
38
- end
35
+ each_with_object self do |(k, v), hsh| hsh[ k ] = yield v end
39
36
  end
40
- alias :do_with_values! :with_values!
41
37
 
42
38
  # The difference from #do_with_values is that modify_values expects block
43
39
  # that takes 2 arguments (key: value pair) and returns the new value.
40
+ #
44
41
  def modify_values
45
42
  each_with_object self.class.new do |hash_pair, ꜧ|
46
43
  ꜧ[ hash_pair[0] ] = yield( hash_pair )
47
44
  end
48
45
  end
49
46
 
50
- # Like #modify_values, but modifies the receiver
47
+ # Like #modify_values, but modifies the receiver.
48
+ #
51
49
  def modify_values!
52
50
  each_with_object self do |hash_pair, ꜧ|
53
51
  ꜧ[ hash_pair[0] ] = yield( hash_pair )
@@ -55,35 +53,37 @@ class Hash
55
53
  end
56
54
 
57
55
  # Like #map that returns a hash.
56
+ #
58
57
  def modify
59
58
  each_with_object self.class.new do |hash_pair, ꜧ|
60
59
  key, val = yield hash_pair
61
60
  ꜧ[key] = val
62
61
  end
63
62
  end
63
+
64
+ # A bit like Array#slice, but only takes 1 argument, which is either a Range,
65
+ # or an Array, and returns the selection of the hash for the keys that match
66
+ # the range or are present in the array.
67
+ #
68
+ def slice matcher
69
+ case matcher
70
+ when Array then select { |key, _| matcher.include? key }
71
+ else select { |key, _| matcher === key } end
72
+ end
64
73
 
65
74
  # Makes hash keys accessible as methods. If the hash keys collide with
66
75
  # its methods, ArgumentError is raised, unless :overwrite_methods
67
76
  # option == true.
68
77
  #
69
- def dot!( oo = {} )
78
+ def dot! overwrite_methods: false
70
79
  keys.each do |key|
71
80
  msg = "key #{key} of #dot!-ted hash is not convertible to a symbol"
72
- raise ArgumentError, msg unless key.respond_to? :to_sym
73
- unless oo[:overwrite_methods]
74
- if methods.include? key.to_sym
75
- raise ArgumentError, "#dot!-ted hash must not have key names " +
76
- "colliding with its methods"
77
- end
78
- end
79
-
80
- define_singleton_method key.to_sym do
81
- self[key]
82
- end
83
-
84
- define_singleton_method "#{key}=".to_sym do |value|
85
- self[key] = value
86
- end
81
+ fail ArgumentError, msg unless key.respond_to? :to_sym
82
+ msg = "#dot!-ted hash must not have key names colliding with its methods"
83
+ fail ArgumentError, msg if methods.include? key.to_sym unless
84
+ overwrite_methods
85
+ define_singleton_method key.to_sym do self[key] end
86
+ define_singleton_method "#{key}=".to_sym do |value| self[key] = value end
87
87
  end
88
88
  return self
89
89
  end
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class Hash
4
+ # Maps a hash into a hash, whose keys have been replaced with names of the
5
+ # key objects (which are assumed to respond to +#name+ method).
6
+ #
7
+ def keys_to_names
8
+ with_keys do |key| key.name || key end
9
+ end
10
+
11
+ # Modifies a hash in place so that the keys are replaced with key names (key
12
+ # objects are assumed to respond to +#name+ method).
13
+ #
14
+ def keys_to_names!
15
+ with_keys! do |key| key.name || key end
16
+ end
17
+ end
@@ -1,7 +1,10 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'y_support'
4
+ require 'y_support/core_ext/hash/misc'
5
+
4
6
  require_relative 'name_magic/array'
7
+ require_relative 'name_magic/hash'
5
8
  require_relative 'name_magic/namespace_methods'
6
9
  require_relative 'name_magic/class_methods'
7
10
 
data/lib/y_support/try.rb CHANGED
@@ -129,4 +129,5 @@ class Object
129
129
  text: attempt_NL_description,
130
130
  &block ).__invoke__
131
131
  end
132
+ alias consciously try
132
133
  end
@@ -1,3 +1,3 @@
1
1
  module YSupport
2
- VERSION = "2.0.21"
2
+ VERSION = "2.0.23"
3
3
  end
data/test/misc_test.rb CHANGED
@@ -34,6 +34,20 @@ describe Module do
34
34
  end
35
35
 
36
36
 
37
+ describe Class do
38
+ before do
39
+ require 'y_support/core_ext/class'
40
+ end
41
+
42
+ it "has #parametrize method" do
43
+ a = Class.new
44
+ -> { a.foo }.must_raise NoMethodError
45
+ b = a.parametrize foo: 42
46
+ b.foo.must_equal 42
47
+ end
48
+ end
49
+
50
+
37
51
  describe Enumerable do
38
52
  before do
39
53
  require 'y_support/core_ext/enumerable'
@@ -94,7 +108,7 @@ describe Array do
94
108
  end
95
109
 
96
110
  it "has #to_column_vector" do
97
- [1, 2, 3].to_column_vector.must_equal Matrix[[1, 2, 3]]
111
+ [1, 2, 3].to_column_vector.must_equal Matrix[[1], [2], [3]]
98
112
  end
99
113
  end
100
114
 
@@ -146,6 +160,13 @@ describe Hash do
146
160
  .modify { |k, v| ["#{k}#{v}".to_sym, "#{v}#{k}"] } )
147
161
  end
148
162
 
163
+ it "should have #slice" do
164
+ { a: 1, b: 2, c: 3 }.slice( [:a, :b] ).must_equal( { a: 1, b: 2 } )
165
+ { 1 => :a, 2 => :b, 3 => :c, 4 => :d }.slice( 2..3.5 )
166
+ .must_equal( { 2 => :b, 3 => :c } )
167
+ { 0.0 => :a, 1.1 => :b }.slice( 1..2 ).must_equal( { 1.1 => :b } )
168
+ end
169
+
149
170
  it "should have #dot! meta patcher for dotted access to keys" do
150
171
  h = Hash.new.merge!(aaa: 1, taint: 2)
151
172
  -> { h.dot! }.must_raise ArgumentError
@@ -58,10 +58,15 @@ describe NameMagic do
58
58
  Rover = @ç.new
59
59
  @ç.const_magic
60
60
  XXX::Rover.must_be_kind_of @ç
61
+ Rover.name.must_equal :Rover
61
62
  @ç.namespace!
62
63
  Spot = @ç.new
63
64
  @ç.const_magic
65
+ Spot.name.must_equal :Spot
64
66
  -> { XXX::Spot }.must_raise NameError
65
67
  @ç.const_get( :Spot ).must_be_kind_of @ç
68
+ # Array
69
+ [ Spot ].names.must_equal [ :Spot ]
70
+ { Spot => 42 }.keys_to_names.must_equal( { Spot: 42 } )
66
71
  end
67
- end
72
+ 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.21
4
+ version: 2.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - boris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-06 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -56,6 +56,8 @@ files:
56
56
  - lib/y_support/core_ext.rb
57
57
  - lib/y_support/core_ext/array.rb
58
58
  - lib/y_support/core_ext/array/misc.rb
59
+ - lib/y_support/core_ext/class.rb
60
+ - lib/y_support/core_ext/class/misc.rb
59
61
  - lib/y_support/core_ext/enumerable.rb
60
62
  - lib/y_support/core_ext/enumerable/misc.rb
61
63
  - lib/y_support/core_ext/hash.rb
@@ -77,6 +79,7 @@ files:
77
79
  - lib/y_support/name_magic.rb
78
80
  - lib/y_support/name_magic/array.rb
79
81
  - lib/y_support/name_magic/class_methods.rb
82
+ - lib/y_support/name_magic/hash.rb
80
83
  - lib/y_support/name_magic/namespace_methods.rb
81
84
  - lib/y_support/null_object.rb
82
85
  - lib/y_support/respond_to.rb