y_support 2.0.20 → 2.0.21

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: b352e0007b5e318e5c95c58725ea49240d283a8d
4
- data.tar.gz: e82fac18c9c94700166f7ce1858bb79f671c5c96
3
+ metadata.gz: 6981d4eb5fdec5cb5eaf09321d4081c39b65c4db
4
+ data.tar.gz: 472b3008961422d3823f0b5b654ca2ecfadbd4f1
5
5
  SHA512:
6
- metadata.gz: 6acfc2dee84cf9e609a2ee271ea67161ee70abb0a912735c8e2b3f8fe37b7c32ab1ed2bb8a8879341bb19b9fd30345cc805d9396100e14fb05e0fa64b951d90d
7
- data.tar.gz: 69c8f221798a650bc8f33093e9f058555c1a8744d3aeb6c83823121d0fac7ecb59d63a693c6256a57efc252af46b973e0d4a7f33e703a9b62de3669c9bf3c6e1
6
+ metadata.gz: c0fe48e25cd93a0b8abce16b12b227d408f1f2e99b5f0275abd13a56735ef78e7b303ad22f18fa9ff15e695f02cd050e3ae32cd3d2130786e9c479ffacc2549f
7
+ data.tar.gz: dbcfa5713ba17cbc1dc64e65f18ad31497faeb82d6943cead57f1504b9e78e1c3ef17bf61d9b18a1684ca8be7e0f224c21b935c16884e3abe2cb0fcc726265a6
@@ -1,3 +1,5 @@
1
+ require 'matrix'
2
+
1
3
  class Array
2
4
  # Converts an array, whose elements are also arrays, to a hash. Head
3
5
  # (position 0) of each array is made to point at the rest of the array
@@ -12,7 +14,6 @@ class Array
12
14
  }
13
15
  end
14
16
 
15
-
16
17
  # Zips this array with another collection into a hash. If a block is given,
17
18
  # it is applied to each element of the array to get the hash values.
18
19
  #
@@ -79,6 +80,12 @@ class Array
79
80
  l = last
80
81
  l.delete( key ).tap { pop if l.empty? } if l.is_a? Hash
81
82
  end
83
+
84
+ # Converts the array to a +Matrix#column_vector+.
85
+ #
86
+ def to_column_vector
87
+ Matrix.column_vector self
88
+ end
82
89
 
83
90
  # TEST ME
84
91
  # def pretty_inspect
@@ -6,12 +6,19 @@ class Array
6
6
  # fails to include the specified element. An optional argument customizes the
7
7
  # error message (element description).
8
8
  #
9
- def aT_includes element, what_is_element=nil
10
- e = what_is_element ? what_is_element.to_s.capitalize :
11
- "Element (#{element.class} instance)"
12
- m = "#{e} is absent from the array."
13
- raise TErr, m unless include? element
14
- return self
9
+ def aT_includes element, what_is_self="array", what_is_element=nil
10
+ m = "%s is absent from #{what_is_self}!" %
11
+ if what_is_element then what_is_element.to_s.capitalize else
12
+ "Element (#{element.class} instance)"
13
+ end
14
+ tap { include? element or fail TypeError, m }
15
15
  end
16
16
  alias :aT_include :aT_includes
17
+
18
+ # Fails with TypeError if the array contains duplicates (using +#uniq+).
19
+ #
20
+ def aT_uniq what_is_self="array"
21
+ m = "#{what_is_self.to_s.capitalize} non-uniq!"
22
+ tap { self == uniq or fail TypeError, m }
23
+ end
17
24
  end
@@ -47,15 +47,13 @@ class Object
47
47
  # checked, whether the object is truey.
48
48
  #
49
49
  def aT what_is_receiver=nil, how_comply=nil, &b
50
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
51
- "#{self.class} instance #{object_id}"
52
- if block_given?
53
- m = "#{r} fails #{how_comply ? 'to %s' % how_comply : 'its duck type'}!"
54
- raise TErr, m unless ( b.arity == 0 ) ? instance_exec( &b ) : b.( self )
55
- else
56
- raise TErr, m unless self
57
- end
58
- return self
50
+ if block_given? then
51
+ m = "%s fails #{how_comply ? 'to %s' % how_comply : 'its duck type'}!" %
52
+ if what_is_receiver then what_is_receiver.to_s.capitalize else
53
+ "#{self.class} instance #{object_id}"
54
+ end
55
+ tap { b.( self ) or fail TypeError, m }
56
+ else self or fail TypeError end
59
57
  end
60
58
 
61
59
  # This method takes a block and fails with TypeError, unless the receiver
@@ -68,28 +66,24 @@ class Object
68
66
  # no block is given, it is checked, whether the object is falsey.
69
67
  #
70
68
  def aT_not what_is_receiver=nil, how_comply=nil, &b
71
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
72
- "#{self.class} instance #{object_id}"
73
- if block_given?
74
- m = how_comply ? "#{r} must not #{how_comply}!" :
75
- "#{r} fails its duck type!"
76
- raise TErr, m if ( b.arity == 0 ) ? instance_exec( &b ) : b.( self )
77
- else
78
- m = "#{r} is not falsey!"
79
- raise TErr, m if self
80
- end
81
- return self
69
+ if block_given? then
70
+ m = how_comply ? "%s must not #{how_comply}!" : "%s fails its duck type!"
71
+ m %= if what_is_receiver then what_is_receiver.to_s.capitalize else
72
+ "#{self.class} instance #{object_id}"
73
+ end
74
+ tap { fail TypeError, m if b.( self ) }
75
+ else tap { fail TypeError if self } end
82
76
  end
83
77
 
84
78
  # Fails with TypeError unless the receiver is of the prescribed class. Second
85
79
  # optional argument customizes the error message (receiver description).
86
80
  #
87
81
  def aT_kind_of klass, what_is_receiver=nil
88
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
89
- "#{self.class} instance #{object_id}"
90
- m = "#{r} is not a kind of #{klass}!"
91
- raise TErr, m unless kind_of? klass
92
- return self
82
+ m = "%s is not a kind of #{klass}!" %
83
+ if what_is_receiver then what_is_receiver.to_s.capitalize else
84
+ "#{self.class} instance #{object_id}"
85
+ end
86
+ tap { kind_of? klass or fail TypeError, m }
93
87
  end
94
88
  alias :aT_is_a :aT_kind_of
95
89
 
@@ -98,11 +92,11 @@ class Object
98
92
  # customizes the error message (receiver description).
99
93
  #
100
94
  def aT_class_complies klass, what_is_receiver=nil
101
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
102
- "#{self.class} instance #{object_id}"
103
- m = "#{r} does not comply or declare compliance with #{klass}!"
104
- raise TErr, m unless class_complies? klass
105
- return self
95
+ m = "%s does not comply or declare compliance with #{klass}!" %
96
+ if what_is_receiver then what_is_receiver.to_s.capitalize else
97
+ "#{self.class} instance #{object_id}"
98
+ end
99
+ tap { class_complies? klass or fail TypeError, m }
106
100
  end
107
101
 
108
102
  # Fails with TypeError unless the receiver responds to the given
@@ -110,11 +104,11 @@ class Object
110
104
  # description).
111
105
  #
112
106
  def aT_respond_to method_name, what_is_receiver=nil
113
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
114
- "#{self.class} instance #{object_id}"
115
- m = "#{r} does not respond to method '#{method_name}'!"
116
- raise TErr, m unless respond_to? method_name
117
- return self
107
+ m = "%s does not respond to method '#{method_name}'!" %
108
+ if what_is_receiver then what_is_receiver.to_s.capitalize else
109
+ "#{self.class} instance #{object_id}"
110
+ end
111
+ tap { respond_to? method_name or fail TypeError, m }
118
112
  end
119
113
  alias :aT_responds_to :aT_respond_to
120
114
 
@@ -126,9 +120,8 @@ class Object
126
120
  r = what_is_receiver ? what_is_receiver.to_s.capitalize :
127
121
  "#{self.class} instance #{object_id}"
128
122
  o = what_is_other || "the prescribed value (#{other.class})"
129
- m = "#{r} is not equal (==) to #{o}!"
130
- raise TErr, m unless self == other
131
- return self
123
+ m = "%s is not equal (==) to %s!" % [r, o]
124
+ tap { self == other or fail TypeError, m }
132
125
  end
133
126
 
134
127
  # Fails with TypeError unless the receiver, according to #== method, differs
@@ -139,31 +132,31 @@ class Object
139
132
  r = what_is_receiver ? what_is_receiver.to_s.capitalize :
140
133
  "#{self.class} instance #{object_id}"
141
134
  o = what_is_other || "the prescribed value (#{other.class})"
142
- m = "#{r} fails to differ from #{o}!"
143
- raise TErr, m if self == other
144
- return self
135
+ m = "%s fails to differ from %s!" % [r, o]
136
+ tap { fail TypeError, m if self == other }
145
137
  end
146
138
 
147
139
  # Fails with TypeError unless the ActiveSupport method #blank returns true
148
140
  # for the receiver.
149
141
  #
150
142
  def aT_blank what_is_receiver=nil
151
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
152
- "#{self.class} instance #{object_id}"
153
- m = "#{r} fails to be #blank?!"
154
- raise TErr, m unless blank?
155
- return self
143
+ r =
144
+ m = "#%s fails to be blank!" %
145
+ if what_is_receiver then what_is_receiver.to_s.capitalize else
146
+ "#{self.class} instance #{object_id}"
147
+ end
148
+ tap { blank? or fail TypeError, m }
156
149
  end
157
150
 
158
151
  # Fails with TypeError unless the ActiveSupport method #present returns true
159
152
  # for the receiver.
160
153
  #
161
154
  def aT_present what_is_receiver=nil
162
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
163
- "#{self.class} instance #{object_id}"
164
- m = "#{r} fails to be #present?!"
165
- raise TErr, m unless present?
166
- return self
155
+ m = "%s fails to be present!" %
156
+ if what_is_receiver then what_is_receiver.to_s.capitalize else
157
+ "#{self.class} instance #{object_id}"
158
+ end
159
+ tap { present? or fail TypeError, m }
167
160
  end
168
161
 
169
162
  private
@@ -1,3 +1,3 @@
1
1
  module YSupport
2
- VERSION = "2.0.20"
2
+ VERSION = "2.0.21"
3
3
  end
data/test/misc_test.rb CHANGED
@@ -92,6 +92,10 @@ describe Array do
92
92
  a.pop_named( :foo ).must_equal 3
93
93
  a.push_ordered( 42 ).must_equal [1, 2, 42, bar: 4]
94
94
  end
95
+
96
+ it "has #to_column_vector" do
97
+ [1, 2, 3].to_column_vector.must_equal Matrix[[1, 2, 3]]
98
+ end
95
99
  end
96
100
 
97
101
  describe Hash do
data/test/typing_test.rb CHANGED
@@ -172,8 +172,8 @@ class TypingTest < Test::Unit::TestCase
172
172
  old = a.dup
173
173
  assert_nothing_raised do a.may_have :z end
174
174
  assert_nothing_raised do a.has? :z end
175
- assert_raises TErr do a.may_have( :a, syn!: :b ) end
176
- assert_raises TErr do a.has?( :a, syn!: :b ) end
175
+ assert_raises TypeError do a.may_have( :a, syn!: :b ) end
176
+ assert_raises TypeError do a.has?( :a, syn!: :b ) end
177
177
  assert_equal false, a.has?( :z )
178
178
  assert_equal nil, a.may_have( :z )
179
179
  assert_equal false, a.has?( :z )
@@ -192,7 +192,7 @@ class TypingTest < Test::Unit::TestCase
192
192
  a = { infile: 'a', csv_out_file: 'b', k: 'k', o: 'k', t: 'k' }
193
193
  assert_respond_to a, :aT_has
194
194
  old = a.dup
195
- assert_raises TErr do a.aT_has :z end
195
+ assert_raises TypeError do a.aT_has :z end
196
196
  assert_nothing_raised do a.aT_has :infile end
197
197
  assert_nothing_raised do a.aT_has :csv_out_file end
198
198
  class TestClass; def initialize( args )
@@ -201,12 +201,12 @@ class TypingTest < Test::Unit::TestCase
201
201
  args.aT_has :k
202
202
  end end
203
203
  assert_nothing_raised do TestClass.new a end
204
- assert_raises TErr do a.aT_has( :a, syn!: :b ) end
204
+ assert_raises TypeError do a.aT_has( :a, syn!: :b ) end
205
205
  assert_equal "a", a.aT_has( :infile )
206
206
  assert_equal "k", a.aT_has( :k, syn!: [:o, :t] )
207
207
  assert_equal "b", a.aT_has( :c, syn!: :csv_out_file )
208
208
  assert_equal( { infile: 'a', c: 'b', k: 'k' }, a )
209
- assert_raises TErr do a.aT_has(:c) {|val| val == 'c'} end
209
+ assert_raises TypeError do a.aT_has(:c) {|val| val == 'c'} end
210
210
  assert_nothing_raised do a.aT_has(:c) {|val| val == 'b'} end
211
211
  end
212
212
  end # context Hash
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.20
4
+ version: 2.0.21
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-04 00:00:00.000000000 Z
11
+ date: 2013-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport