stupidedi 1.2.14 → 1.2.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -6
  3. data/lib/ruby/jruby_hack.rb +683 -0
  4. data/lib/stupidedi.rb +4 -0
  5. data/lib/stupidedi/config.rb +1 -0
  6. data/lib/stupidedi/contrib/004010/guides.rb +1 -0
  7. data/lib/stupidedi/contrib/004010/guides/GF990.rb +6 -1
  8. data/lib/stupidedi/contrib/004010/guides/IM210.rb +267 -0
  9. data/lib/stupidedi/contrib/004010/guides/QM214.rb +17 -5
  10. data/lib/stupidedi/contrib/004010/guides/SM204.rb +252 -57
  11. data/lib/stupidedi/contrib/004010/transaction_set_defs.rb +3 -0
  12. data/lib/stupidedi/contrib/004010/transaction_set_defs/IM210.rb +47 -0
  13. data/lib/stupidedi/contrib/004010/transaction_set_defs/SM204.rb +20 -2
  14. data/lib/stupidedi/version.rb +1 -1
  15. data/lib/stupidedi/versions/functional_groups/004010/element_defs.rb +115 -4
  16. data/lib/stupidedi/versions/functional_groups/004010/segment_defs.rb +31 -0
  17. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/B3.rb +31 -0
  18. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/C3.rb +22 -0
  19. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/H3.rb +25 -0
  20. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/L0.rb +34 -0
  21. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/L1.rb +42 -0
  22. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/L3.rb +1 -1
  23. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/L4.rb +25 -0
  24. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/L7.rb +35 -0
  25. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/LAD.rb +39 -0
  26. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/MEA.rb +1 -1
  27. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/N9.rb +7 -7
  28. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/OID.rb +33 -0
  29. data/lib/stupidedi/versions/functional_groups/004010/segment_defs/R3.rb +28 -0
  30. data/lib/stupidedi/versions/functional_groups/005010/segment_defs/HI.rb +2 -2
  31. data/lib/stupidedi/versions/functional_groups/005010/transaction_set_defs/HC837.rb +2 -1
  32. data/spec/examples/ruby/symbol.example +20 -23
  33. data/spec/examples/stupidedi/versions/005010/element_types/nn.example +4 -3
  34. data/spec/examples/stupidedi/versions/005010/element_types/r.example +5 -4
  35. metadata +34 -46
  36. data/spec/coverage/assets/0.10.0/application.css +0 -799
  37. data/spec/coverage/assets/0.10.0/application.js +0 -1707
  38. data/spec/coverage/assets/0.10.0/colorbox/border.png +0 -0
  39. data/spec/coverage/assets/0.10.0/colorbox/controls.png +0 -0
  40. data/spec/coverage/assets/0.10.0/colorbox/loading.gif +0 -0
  41. data/spec/coverage/assets/0.10.0/colorbox/loading_background.png +0 -0
  42. data/spec/coverage/assets/0.10.0/favicon_green.png +0 -0
  43. data/spec/coverage/assets/0.10.0/favicon_red.png +0 -0
  44. data/spec/coverage/assets/0.10.0/favicon_yellow.png +0 -0
  45. data/spec/coverage/assets/0.10.0/loading.gif +0 -0
  46. data/spec/coverage/assets/0.10.0/magnify.png +0 -0
  47. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  48. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  49. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  50. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  51. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  52. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  53. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  54. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  55. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
  56. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  57. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
  58. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
  59. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  60. data/spec/coverage/index.html +0 -72
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e0bed8435395120843b4f51683cea5d686732e6
4
- data.tar.gz: c5bb0aee47784bca3225adaa3cab3baf8fdc4635
3
+ metadata.gz: e0fed8a672c59ba62f160e4114d2e28b2e400081
4
+ data.tar.gz: 2c5d48eef8a2f71ab54a1c297b4959a39f2c11ce
5
5
  SHA512:
6
- metadata.gz: '08bb934927e0f00a6ace691da761ff6d283c0786ffb2681d0077864b8fd0e66c28e7529d31c100a8a6987c0f7f7b277a21a26688f5dcbbdfcc9c4cccc0bbd510'
7
- data.tar.gz: f4d19956f28deed5d76affa8c980a17d320981e88fdd3cb085dd4fdac4774fd35c7048b5ee42c6a0fb6fde4a629dbb25863ec699e2abbc920b68073ab62bd041
6
+ metadata.gz: 5b687f855fbeb62b9012045fa408d222d016f8e9b5d14373d139b4262602ff9f0a96a5ee19dae8241c24cfdc33edb4981170feb821e2cda5b75b1edd33eb2c48
7
+ data.tar.gz: 4532f6ef471e4f93fe415503c1609c0fed610544d9daa70b58c291931ec97c88d2b21860e59eaafd46a388591cf172515e2f28638dbc20c0a645070bc7ec150b
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # Stupidedi
2
- [![Build Status](https://secure.travis-ci.org/kputnam/stupidedi.png?branch=master)](http://travis-ci.org/kputnam/stupidedi) [![Dependency Status](https://gemnasium.com/irobayna/stupidedi.svg)](https://gemnasium.com/irobayna/stupidedi) [![GitHub version](https://badge.fury.io/gh/kputnam%2Fstupidedi.svg)](http://badge.fury.io/gh/kputnam%2Fstupidedi) [![Code Climate](https://codeclimate.com/github/kputnam/stupidedi.png)](https://codeclimate.com/github/kputnam/stupidedi) [![Inline docs](http://inch-ci.org/github/kputnam/stupidedi.png?branch=master)](http://inch-ci.org/github/kputnam/stupidedi)
2
+ [![Build Status](https://secure.travis-ci.org/irobayna/stupidedi.png?branch=master)](http://travis-ci.org/irobayna/stupidedi) [![Dependency Status](https://gemnasium.com/irobayna/stupidedi.svg)](https://gemnasium.com/irobayna/stupidedi) [![GitHub version](https://badge.fury.io/gh/irobayna%2Fstupidedi.svg)](http://badge.fury.io/gh/irobayna%2Fstupidedi) [![Code Climate](https://codeclimate.com/github/irobayna/stupidedi.png)](https://codeclimate.com/github/irobayna/stupidedi) [![Inline docs](http://inch-ci.org/github/irobayna/stupidedi.png?branch=master)](http://inch-ci.org/github/irobayna/stupidedi)
3
3
 
4
- ![Screenshot](https://raw.github.com/kputnam/stupidedi/master/doc/images/edi-pp.png)
4
+ ![Screenshot](https://raw.github.com/irobayna/stupidedi/master/doc/images/edi-pp.png)
5
5
 
6
6
 
7
- * [GitHub project](http://github.com/kputnam/stupidedi)
8
- * [Human Documentation](https://github.com/kputnam/stupidedi/tree/master/doc)
9
- * [API Documentation](http://rubydoc.info/github/kputnam/stupidedi/master/frames)
7
+ * [GitHub project](http://github.com/irobayna/stupidedi)
8
+ * [Human Documentation](https://github.com/irobayna/stupidedi/tree/master/doc)
9
+ * [API Documentation](http://rubydoc.info/github/irobayna/stupidedi/master/frames)
10
10
 
11
11
  Stupidedi is a high-quality library for parsing, generating, validating,
12
12
  and manipulating ASC X12 EDI documents. Very roughly, it's jQuery for
@@ -81,7 +81,7 @@ immutability places higher demand on garbage collection, this has been
81
81
  mitigated with careful optimization. Input can be streamed incrementally, so
82
82
  very large files aren't read into memory all at once.
83
83
 
84
- ![Benchmark](https://raw.github.com/kputnam/stupidedi/master/notes/benchmark/throughput.png)
84
+ ![Benchmark](https://raw.github.com/irobayna/stupidedi/master/notes/benchmark/throughput.png)
85
85
 
86
86
  <table>
87
87
  <tr>
@@ -0,0 +1,683 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/ruby/array.rb
4
+ class Array
5
+ def blank?
6
+ empty?
7
+ end
8
+
9
+ def present?
10
+ not empty?
11
+ end
12
+
13
+ # Return the first item. Raises an `IndexError` if the Array is `empty?`.
14
+ #
15
+ # @example
16
+ # [1, 2, 3].head #=> 1
17
+ #
18
+ def head
19
+ raise IndexError, "head of empty list" if empty?
20
+ x, = self
21
+ x
22
+ end
23
+
24
+ # True if `#at` is defined for the given `n`
25
+ #
26
+ # @example
27
+ # [1, 2, 3].defined_at?(0) #=> true
28
+ # [].defined_at?(0) #=> false
29
+ #
30
+ def defined_at?(n)
31
+ n < length and -n <= length
32
+ end
33
+
34
+ # @group Selection
35
+ #############################################################################
36
+
37
+ # Selects all elements except the first.
38
+ #
39
+ # @example
40
+ # [1, 2, 3].tail #=> [2, 3]
41
+ # [1].tail #=> []
42
+ # [].tail #=> []
43
+ #
44
+ # @return [Array]
45
+ def tail
46
+ _, *xs = self
47
+ xs
48
+ end
49
+
50
+ # Selects all elements except the last `n` ones.
51
+ #
52
+ # @example
53
+ # [1, 2, 3].init #=> [1, 2]
54
+ # [1, 2, 3].init(2) #=> [1]
55
+ # [].tail #=> []
56
+ #
57
+ # @return [Array]
58
+ def init(n = 1)
59
+ raise ArgumentError, "n cannot be negative" if n < 0
60
+ slice(0..-(n + 1)) or []
61
+ end
62
+
63
+ # Select all elements except the first `n` ones.
64
+ #
65
+ # @example
66
+ # [1, 2, 3].drop(1) #=> [2, 3]
67
+ # [1, 3, 3].drop(2) #=> [3]
68
+ # [].drop(10) #=> []
69
+ #
70
+ # @return [Array]
71
+ def drop(n)
72
+ raise ArgumentError, "n cannot be negative" if n < 0
73
+ slice(n..-1) or []
74
+ end
75
+
76
+ # Select the first `n` elements.
77
+ #
78
+ # @example
79
+ # [1, 2, 3].take(2) #=> [1, 2]
80
+ # [1, 2, 3].take(0) #=> []
81
+ #
82
+ # @return [Array]
83
+ def take(n)
84
+ raise ArgumentError, "n cannot be negative" if n < 0
85
+ slice(0, n) or []
86
+ end
87
+
88
+ # Split the array in two at the given position.
89
+ #
90
+ # @example
91
+ # [1, 2, 3].split_at(2) #=> [[1,2], [3]]
92
+ # [1, 2, 3].split_at(0) #=> [[], [1,2,3]]
93
+ #
94
+ # @return [(Array, Array)]
95
+ def split_at(n)
96
+ n = length + n if n < 0
97
+ return take(n), drop(n)
98
+ end
99
+
100
+ # @endgroup
101
+ #############################################################################
102
+
103
+ # @group Filtering
104
+ #############################################################################
105
+
106
+ # Drops the longest prefix of elements that satisfy the predicate.
107
+ #
108
+ # @return [Array]
109
+ def drop_while(&block)
110
+ # This is in tail call form
111
+ if not empty? and yield(head)
112
+ tail.drop_while(&block)
113
+ else
114
+ self
115
+ end
116
+ end
117
+
118
+ # Drops the longest prefix of elements that do not satisfy the predicate.
119
+ #
120
+ # @return [Array]
121
+ def drop_until(&block)
122
+ # This is in tail call form
123
+ unless empty? or yield(head)
124
+ tail.drop_until(&block)
125
+ else
126
+ self
127
+ end
128
+ end
129
+
130
+ # Takes the longest prefix of elements that satisfy the predicate.
131
+ #
132
+ # @return [Array]
133
+ def take_while(accumulator = [], &block)
134
+ # This is in tail call form
135
+ if not empty? and yield(head)
136
+ tail.take_while(head.snoc(accumulator), &block)
137
+ else
138
+ accumulator
139
+ end
140
+ end
141
+
142
+ # Takes the longest prefix of elements that do not satisfy the predicate.
143
+ #
144
+ # @return [Array]
145
+ def take_until(accumulator = [], &block)
146
+ # This is in tail call form
147
+ unless empty? or yield(head)
148
+ tail.take_until(head.snoc(accumulator), &block)
149
+ else
150
+ accumulator
151
+ end
152
+ end
153
+
154
+ # Splits the array into prefix/suffix pair according to the predicate.
155
+ #
156
+ # @return [(Array, Array)]
157
+ def split_until(&block)
158
+ prefix = take_while(&block)
159
+ suffix = drop(prefix.length)
160
+ return prefix, suffix
161
+ end
162
+
163
+ # Splits the array into prefix/suffix pair according to the predicate.
164
+ #
165
+ # @return [(Array, Array)]
166
+ def split_when(&block)
167
+ prefix = take_until(&block)
168
+ suffix = drop(prefix.length)
169
+ return prefix, suffix
170
+ end
171
+
172
+ # Returns a list of sublists, where each sublist contains only equal
173
+ # elements. Equality is determined by a two-argument block parameter.
174
+ # The concatenation of the result is equal to the original argument.
175
+ #
176
+ # @example
177
+ # "abba".split(//).group_seq(&:==) #=> [["y"], ["a"], ["b", "b"], ["a"]]
178
+ #
179
+ # @return [[Array]]
180
+ def runs(&block)
181
+ unless empty?
182
+ as, bs = tail.split_until{|x| block.call(head, x) }
183
+ head.cons(as).cons(bs.runs(&block))
184
+ else
185
+ []
186
+ end
187
+ end
188
+
189
+ # @endgroup
190
+ #############################################################################
191
+
192
+ # Accumulate elements using the `+` method, optionally
193
+ # transforming them first using a block
194
+ #
195
+ # @example
196
+ # ["a", "b", "cd"].sum #=> "abcd"
197
+ # ["a", "b", "cd"].sum(&:length) #=> 4
198
+ #
199
+ def sum(&block)
200
+ if block_given?
201
+ tail.inject(yield(head)){|sum,e| sum + yield(e) }
202
+ else
203
+ tail.inject(head){|sum,e| sum + e }
204
+ end
205
+ end
206
+
207
+ # Count the number of elements that satisfy the predicate
208
+ #
209
+ # @example
210
+ # ["abc", "de", "fg", "hi"].count{|s| s.length == 2 } #=> 3
211
+ # ["a", "b", "a", "c", "a"].count("a") #=> 3
212
+ # [1, 3, 5, 9, 0].count #=> 5
213
+ #
214
+ # @return [Integer]
215
+ def count(*args)
216
+ if block_given?
217
+ inject(0){|n, e| yield(e) ? n + 1 : n }
218
+ elsif args.empty?
219
+ size
220
+ else
221
+ inject(0){|n, e| e == args.first ? n + 1 : n }
222
+ end
223
+ end
224
+ end
225
+
226
+ # lib/ruby/blank.rb
227
+ class String
228
+ # True if the string is `empty?` or contains all whitespace
229
+ #
230
+ # @example
231
+ # "abc".blank? #=> false
232
+ # " ".blank? #=> true
233
+ # "".blank? #=> true
234
+ #
235
+ def blank?
236
+ self !~ /\S/
237
+ end
238
+
239
+ def present?
240
+ self =~ /\S/
241
+ end
242
+ end
243
+
244
+ class NilClass
245
+ # Always `true`. Note this overrides {Object#blank?} which returns false.
246
+ #
247
+ # @example
248
+ # nil.blank? #=> true
249
+ #
250
+ def blank?
251
+ true
252
+ end
253
+
254
+ def present?
255
+ false
256
+ end
257
+ end
258
+
259
+ class Object
260
+ # Always `false`. Note that {NilClass#blank?} is overridden to return `true`
261
+ #
262
+ # @example
263
+ # false.blank? #=> false
264
+ # 100.blank? #=> false
265
+ #
266
+ def blank?
267
+ false
268
+ end
269
+
270
+ def present?
271
+ true
272
+ end
273
+ end
274
+
275
+ # lib/ruby/hash.rb
276
+ class Hash
277
+ def defined_at?(x)
278
+ include?(x)
279
+ end
280
+
281
+ def at(x)
282
+ self[x]
283
+ end
284
+ end
285
+
286
+ # lib/ruby/instance_exec.rb
287
+ class Object
288
+ if RUBY_VERSION < "1.9"
289
+ module InstanceExecHelper; end
290
+ include InstanceExecHelper
291
+
292
+ # @see http://eigenclass.org/hiki/instance_exec
293
+ def instance_exec(*args, &block)
294
+ thread = Thread.current.object_id.abs
295
+ object = object_id.abs
296
+ mname = "__instance_exec_#{thread}_#{object}"
297
+ InstanceExecHelper.module_eval { define_method(mname, &block) }
298
+
299
+ begin
300
+ __send__(mname, *args)
301
+ ensure
302
+ begin
303
+ InstanceExecHelper.module_eval { remove_method(mname) }
304
+ rescue
305
+ end
306
+ end
307
+ end
308
+ end
309
+ end
310
+
311
+ # lib/ruby/module.rb
312
+ class Module
313
+ # Creates an abstract method
314
+ #
315
+ # @example
316
+ # class Collection
317
+ # abstract :size
318
+ # abstract :add, :args => %w(item)
319
+ # end
320
+ #
321
+ # @return [void]
322
+ def abstract(name, *params)
323
+ if params.last.is_a?(Hash)
324
+ # abstract :method, :args => %w(a b c)
325
+ params = params.last[:args]
326
+ end
327
+
328
+ file, line, = Stupidedi.caller
329
+
330
+ if params.empty?
331
+ class_eval(<<-RUBY, file, line.to_i - 1)
332
+ def #{name}(*args)
333
+ raise NoMethodError,
334
+ "method \#{self.class.name}.#{name} is abstract"
335
+ end
336
+ RUBY
337
+ else
338
+ class_eval(<<-RUBY, file, line.to_i - 1)
339
+ def #{name}(*args)
340
+ raise NoMethodError,
341
+ "method \#{self.class.name}.#{name}(#{params.join(', ')}) is abstract"
342
+ end
343
+ RUBY
344
+ end
345
+ end
346
+
347
+ def def_delegators(target, *methods)
348
+ file, line, = Stupidedi.caller
349
+
350
+ for m in methods
351
+ if m.to_s =~ /=$/
352
+ class_eval(<<-RUBY, file, line.to_i - 1)
353
+ def #{m}(value)
354
+ #{target}.#{m}(value)
355
+ end
356
+ RUBY
357
+ else
358
+ class_eval(<<-RUBY, file, line.to_i - 1)
359
+ def #{m}(*args, &block)
360
+ #{target}.#{m}(*args, &block)
361
+ end
362
+ RUBY
363
+ end
364
+ end
365
+ end
366
+ end
367
+
368
+ # lib/ruby/object.rb
369
+ class Object
370
+ # @group List Constructors
371
+ #############################################################################
372
+
373
+ # Prepend the item to the front of a new list
374
+ #
375
+ # @example
376
+ # 1.cons #=> [1]
377
+ # 1.cons(2.cons) #=> [1, 2]
378
+ # 1.cons([0, 0, 0]) #=> [1, 0, 0, 0]
379
+ #
380
+ # @return [Array]
381
+ def cons(array = [])
382
+ [self] + array
383
+ end
384
+
385
+ # Append the item to rear of a new list
386
+ #
387
+ # @example
388
+ # 1.snoc #=> [1]
389
+ # 1.snoc(2.snoc) #=> [2, 1]
390
+ # 1.snoc([0, 0, 0]) #=> [0, 0, 0, 1]
391
+ #
392
+ # @return [Array]
393
+ def snoc(array = [])
394
+ array + [self]
395
+ end
396
+
397
+ # @group Combinators
398
+ #############################################################################
399
+
400
+ # Yields `self` to a block argument
401
+ #
402
+ # @example
403
+ # nil.bind{|a| a.nil? } #=> true
404
+ # 100.bind{|a| a.nil? } #=> false
405
+ #
406
+ def bind
407
+ yield self
408
+ end
409
+
410
+ # Yields `self` to a side-effect block argument and return `self`
411
+ #
412
+ # @example:
413
+ # 100.tap{|a| puts "debug: #{a}" } #=> 100
414
+ #
415
+ # @return self
416
+ def tap
417
+ yield self
418
+ self
419
+ end unless nil.respond_to?(:tap)
420
+
421
+ # @endgroup
422
+ #############################################################################
423
+
424
+ # Return the "eigenclass" where singleton methods reside
425
+ #
426
+ # @return [Class]
427
+ def eigenclass
428
+ class << self; self; end
429
+ end
430
+ end
431
+
432
+ # lib/ruby/string.rb
433
+ class String
434
+ # Return the one-character string at the given index
435
+ #
436
+ # @example
437
+ # "abc".at(0) #=> "a"
438
+ # "abc".at(2) #=> "c"
439
+ #
440
+ # @param [Integer] n zero-based index of the character to read
441
+ #
442
+ # @return [String]
443
+ def at(n)
444
+ raise ArgumentError, "n must be positive" if n < 0
445
+ self[n, 1] unless n >= length
446
+ end
447
+
448
+ # Return the string with `n` characters removed from the front
449
+ #
450
+ # @example
451
+ # "abc".drop(0) #=> "abc"
452
+ # "abc".drop(2) #=> "c"
453
+ #
454
+ # @param [Integer] n number of characters to drop (`n > 0`)
455
+ #
456
+ # @return [String]
457
+ def drop(n)
458
+ raise ArgumentError, "n must be positive" if n < 0
459
+ (length >= n) ? self[n..-1] : ""
460
+ end
461
+
462
+ # Return the first `n` characters from the front
463
+ #
464
+ # @example
465
+ # "abc".take(0) #=> ""
466
+ # "abc".take(2) #=> "ab"
467
+ #
468
+ # @param [Integer] n number of characters to select (`n > 0`)
469
+ #
470
+ # @return [String]
471
+ def take(n)
472
+ raise ArgumentError, "n must be positive" if n < 0
473
+ self[0, n]
474
+ end
475
+
476
+ # Split the string in two at the given position
477
+ #
478
+ # @example
479
+ # "abc".split_at(0) #=> ["", "abc"]
480
+ # "abc".split_at(2) #=> ["ab", "c"]
481
+ #
482
+ # @param [Integer] n number of characters at which to split (`n > 0`)
483
+ #
484
+ # @return [Array(String, String)]
485
+ def split_at(n)
486
+ [take(n), drop(n)]
487
+ end
488
+
489
+ # True if the string is long enough such that {#at} is defined for the
490
+ # given `n`
491
+ #
492
+ # @example
493
+ # "abc".defined_at?(0) #=> true
494
+ # "abc".defined_at?(3) #=> false
495
+ def defined_at?(n)
496
+ n < length
497
+ end
498
+
499
+ # To make String compatible with the {Stupidedi::Reader::Input} interface,
500
+ # we have to define `#position`... shameful!
501
+ def position
502
+ nil
503
+ end
504
+ end
505
+
506
+ # lib/ruby/symbol.rb
507
+ class Symbol
508
+ # Returns a proc that calls self on the proc's parameter
509
+ #
510
+ # @example
511
+ # [1, 2, 3].map(&:-@) #=> [-1, -2, -3]
512
+ # [-1, -2, -3].map(&:abs) #=> [1, 2, 3]
513
+ #
514
+ def to_proc
515
+ lambda{|*args| args.head.__send__(self, *args.tail) }
516
+ end
517
+
518
+ # Calls self on the given receiver
519
+ #
520
+ # @example
521
+ # :to_s.call(100) #=> "100"
522
+ # :join.call([1,2,3], "-") #=> "1-2-3"
523
+ #
524
+ def call(receiver, *args)
525
+ receiver.__send__(self, *args)
526
+ end
527
+ end
528
+
529
+ # lib/ruby/to_d.rb
530
+ class BigDecimal
531
+ # @return [BigDecimal] self
532
+ def to_d
533
+ self
534
+ end
535
+ end
536
+
537
+ class String
538
+ BIGDECIMAL = /\A[+-]? (?# optional leading sign )
539
+ (?:
540
+ (?:\d+\.?\d*) | (?# whole with optional decimal or ..)
541
+ (?:\d*?\.?\d+) ) (?# optional whole with decimal )
542
+ (?:E[+-]?\d+)? (?# optional exponent )
543
+ \Z/ix
544
+
545
+ # Converts the string to a BigDecimal after validating the format. If the
546
+ # string does not match the pattern for a valid number, an `ArgumentError`
547
+ # is raised.
548
+ #
549
+ # @example
550
+ # "1.0".to_d #=> BigDecimal("1.0")
551
+ #
552
+ # @return [BigDecimal]
553
+ def to_d
554
+ if BIGDECIMAL =~ self
555
+ BigDecimal(to_s)
556
+ else
557
+ raise ArgumentError, "#{inspect} is not a valid number"
558
+ end
559
+ end
560
+ end
561
+
562
+ class Integer
563
+ # Converts the integer to a BigDecimal
564
+ #
565
+ # @example
566
+ # 10.to_d #=> BigDecimal("10")
567
+ #
568
+ # @return [BigDecimal]
569
+ def to_d
570
+ BigDecimal(to_s)
571
+ end
572
+ end
573
+
574
+ class Rational
575
+ # Converts the rational to a BigDecimal
576
+ #
577
+ # @example
578
+ # Rational(3, 4).to_d #=> BigDecimal("3") / BigDecimal("4")
579
+ #
580
+ # @return [BigDecimal]
581
+ def to_d
582
+ numerator.to_d / denominator.to_d
583
+ end
584
+ end
585
+
586
+ class Float
587
+ # Raises a `TypeError` exception. The reason this method is defined at
588
+ # all is to produce a more meaningful error than `NoSuchMethod`.
589
+ #
590
+ # @return [void]
591
+ def to_d
592
+ # The problem is there isn't a way to know the correct precision,
593
+ # since there are (many) values that cannot be represented exactly
594
+ # using Floats. For instance, we can't assume which value is correct
595
+ #
596
+ # "%0.10f" % 1.8 #=> "1.8000000000"
597
+ # "%0.20f" % 1.8 #=> "1.80000000000000004441"
598
+ #
599
+ # The programmer should convert the Float to a String using whatever
600
+ # precision he chooses, and call #to_d on the String.
601
+ raise TypeError, "cannot convert Float to BigDecimal"
602
+ end
603
+ end
604
+
605
+ # lib/ruby/to_date.rb
606
+ class Time
607
+ def to_date
608
+ Date.civil(year, month, day)
609
+ end
610
+ end
611
+
612
+ class String
613
+ def to_date
614
+ Date.parse(self)
615
+ end
616
+ end
617
+
618
+ class Date
619
+ def to_date
620
+ self
621
+ end
622
+ end
623
+
624
+ # lib/ruby/to_time.rb
625
+ class Time
626
+ # @return [Time]
627
+ def to_time
628
+ self
629
+ end
630
+ end
631
+
632
+ class << Time
633
+ public :parse
634
+ end
635
+
636
+ class String
637
+ def to_time
638
+ Time.parse(self)
639
+ end
640
+ end
641
+
642
+ # lib/ruby/try.rb
643
+ class Object
644
+ # @group Combinators
645
+ #############################################################################
646
+
647
+ # Sends the arguments to `self` or yields `self` (when `self` is non-`nil`).
648
+ # This is overridden by {NilClass#try}, which always returns `nil`.
649
+ #
650
+ # @example
651
+ # "non-nil".try(&:length) #=> 7
652
+ # nil.try(&:length) #=> nil
653
+ #
654
+ # "non-nil".try(:slice, 0, 3) #=> "non"
655
+ # nil.try(:slice, 0, 3) #=> nil
656
+ #
657
+ def try(*args, &block)
658
+ if args.empty?
659
+ yield self
660
+ else
661
+ __send__(*args, &block)
662
+ end
663
+ end
664
+ end
665
+
666
+ class NilClass
667
+ # @group Combinators
668
+ #############################################################################
669
+
670
+ # Returns `nil` (when `self` is `nil`). This overrides {Object#try}
671
+ #
672
+ # @example
673
+ # "non-nil".try(&:length) #=> 7
674
+ # nil.try(&:length) #=> nil
675
+ #
676
+ # "non-nil".try(:slice, 0, 3) #=> "non"
677
+ # nil.try(:slice, 0, 3) #=> nil
678
+ #
679
+ # @return nil
680
+ def try(*args)
681
+ self
682
+ end
683
+ end