sleeping_king_studios-ext 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a577560ebfa76eda2fb92e99731fd660d46ef67c
4
- data.tar.gz: 08f133c3c52f49a36b977e42da1fd293d24dbaec
3
+ metadata.gz: fc326e56033ad5f5c95eb5b7a7ff96d7ad6ec487
4
+ data.tar.gz: 1f533dac8c8088c63a786dcf2f9c1fc8c5531c4c
5
5
  SHA512:
6
- metadata.gz: 1abefaf6b82006133c1820a32f38939ca43836fa9a06aa3804ccae2f9f001a37f98f63f8e1292d256512007d7bbec32f3cf21e6b9a447d907bed914d068e1a03
7
- data.tar.gz: 77753ec2b8bea81cc240e45d5c011f2f62e3a396925acd5c8b4e1ef4bba3a5f58318e37f1537e67ee430a1c6bbd6050bf83c5a3be80db17e539166d667364f75
6
+ metadata.gz: aebc561b0ec814631dc6c70206bde62a7c3505c8663053d48979ae06072c1c4dc0bacd7fb4d7962035703aeeb5f3485e8828b23bce27899558243327c0d9d912
7
+ data.tar.gz: 2ec1bbe818e887f7741d1ebadf296c045a834f05292189c55059944f1e76e81de7e4d9a9d76d4adc649935c699235a491d8dcd539cde99b542c4bb710425df9e
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Integer
6
+
7
+ - Added Integer#digits method.
8
+ - Added Integer#romanize method.
9
+
3
10
  ## 0.1.0
4
11
 
5
12
  Initial release version.
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  A collection of extensions to core classes.
4
4
 
5
+ YARD documentation is available at
6
+ [RubyDoc.info](http://rubydoc.info/github/sleepingkingstudios/sleeping_king_studios-ext).
7
+
5
8
  ## The Extensions
6
9
 
7
10
  ### Array\#fold
@@ -14,7 +17,7 @@ result of yielding the block is counted instead.
14
17
 
15
18
  _Example:_ Find the number of times each item appears in the array.
16
19
 
17
- items = %w(foo foo foo bar bar baz wibble wobble)
20
+ items = %w(foo foo foo bar bar baz wibble wobble)
18
21
  items.fold
19
22
  #=> { 'foo' => 3, 'bar' => 2, 'baz' => 1, 'wibble' => 1, 'wobble' => 1 }
20
23
 
@@ -33,6 +36,59 @@ _Example:_ Count the items in the array with each status value.
33
36
  items.fold { |item| item.status }
34
37
  #=> { :approved => 3, :rejected => 2, :error => 1, nil => 1 }
35
38
 
39
+ ### Integer\#digits
40
+
41
+ require 'sleeping_king_studios/ext/integer/digits'
42
+
43
+ Breaks the number into its component digits in the specified base.
44
+
45
+ _Example:_ Get the digits of a number in base 10.
46
+
47
+ 23154.digits
48
+ #=> ['2', '3', '1', '5', '4']
49
+
50
+ _Example:_ Get the digits of a number in base 2.
51
+
52
+ 80.digits(2)
53
+ #=> ['1', '0', '1', '0', '0', '0', '0']
54
+
55
+ _Example:_ Get digits of a number in base 16.
56
+
57
+ 3894.digits(16)
58
+ #=> ['f', '3', '6']
59
+
60
+ ### Integer\#romanize
61
+
62
+ require 'sleeping_king_studios/ext/integer/romanize'
63
+
64
+ Returns the value as a string of Roman numerals, i.e. the number 7 becomes the
65
+ string "VII". The number must be greater than 0 and less than 5000.
66
+
67
+ _Example:_ Get the number's representation in Roman numerals.
68
+
69
+ 7.romanize
70
+ #=> 'VII'
71
+
72
+ 18.romanize
73
+ #=> 'XVIII'
74
+
75
+ 49.romanize
76
+ #=> 'XLIX'
77
+
78
+ 3894.romanize
79
+ #=> 'MMMDCCCXCIV'
80
+
81
+ #### Additive
82
+
83
+ If the additive flag is set to true, uses the form 'IIII' for 4 and 'VIIII'
84
+ for 9, as well instead of 'IV' and 'IX', respectively. Also applies to the tens
85
+ and hundreds places, e.g. 40, 90, 400, and 900.
86
+
87
+ _Example:_ Get the number's representation in additive Roman numerals.
88
+
89
+ 499.romanize :additive => true
90
+ #=> 'CCCCLXXXXVIIII'
91
+
36
92
  ### Object\#metaclass
37
93
 
38
94
  require 'sleeping_king_studios/ext/object/metaclass'
@@ -0,0 +1,12 @@
1
+ # lib/sleeping_king_studios/ext/integer/digits.rb
2
+
3
+ class Integer
4
+ # Breaks the number into its component digits in the specified base.
5
+ #
6
+ # @param [Integer] base The base to use when breaking down the number.
7
+ #
8
+ # @return [Array<String>]
9
+ def digits base = 10
10
+ to_s(base).split ''
11
+ end # method digits
12
+ end # class
@@ -0,0 +1,64 @@
1
+ # lib/sleeping_king_studios/ext/integer/romanize.rb
2
+
3
+ require 'sleeping_king_studios/ext/integer/digits'
4
+
5
+ class Integer
6
+ # Minimum allowed value for the #romanize method.
7
+ ROMANIZE_MIN = 1
8
+
9
+ # Maximum allowed value for the #romanize method.
10
+ ROMANIZE_MAX = 4999
11
+
12
+ # Returns the value as a string of Roman numerals. The number must be greater
13
+ # than 0 and less than 5000 (numbers between 4000 and 4999 are represented by
14
+ # the string MMMM in the thousands place).
15
+ #
16
+ # @param [Hash] options The romanization options.
17
+ # @option options [Boolean] additive If true, uses the structure IIII for 4
18
+ # and the VIIII structure for 9 instead of IV and IX, respectively. Also
19
+ # applies to the tens and hundreds places, e.g. 40, 90, 400, etc (the
20
+ # thousands place is always additive).
21
+ #
22
+ # @return [String] The Roman numerals.
23
+ #
24
+ # @raise [RangeError] If the number is too large or too small to represent
25
+ # using Roman numerals.
26
+ def romanize options = {}
27
+ unless (ROMANIZE_MIN..ROMANIZE_MAX).include? self
28
+ raise RangeError.new "integer to romanize must be within range #{ROMANIZE_MIN} to #{ROMANIZE_MAX}"
29
+ end # unless
30
+
31
+ rules = [
32
+ '',
33
+ '%one',
34
+ '%one%one',
35
+ '%one%one%one',
36
+ '%one%five',
37
+ '%five',
38
+ '%five%one',
39
+ '%five%one%one',
40
+ '%five%one%one%one',
41
+ '%one%ten',
42
+ '%ten'
43
+ ] # end array rules
44
+
45
+ if Hash === options && options.fetch(:additive, false)
46
+ rules[4] = '%one%one%one%one'
47
+ rules[9] = '%five%one%one%one%one'
48
+ end # if
49
+
50
+ numerals = [
51
+ %w(I V X),
52
+ %w(X L C),
53
+ %w(C D M),
54
+ ['M', 'MMM', '']
55
+ ] # end array numerals
56
+
57
+ digits.reverse.map.with_index do |digit, index|
58
+ rules[digit.to_i]
59
+ .gsub('%one', numerals[index][0])
60
+ .gsub('%five', numerals[index][1])
61
+ .gsub('%ten', numerals[index][2])
62
+ end.reverse.join ''
63
+ end # method romanize
64
+ end # class
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SleepingKingStudios
4
4
  module Ext
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end # module
7
7
  end # module
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sleeping_king_studios-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob "Merlin" Smith
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.8.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-sleeping_king_studios
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0.rc.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0.rc.3
55
69
  description: |2
56
70
  A collection of extensions to core classes, gemified to prevent a
57
71
  maintenance nightmare if multiple library or application projects require
@@ -63,6 +77,8 @@ extensions: []
63
77
  extra_rdoc_files: []
64
78
  files:
65
79
  - lib/sleeping_king_studios/ext/array/fold.rb
80
+ - lib/sleeping_king_studios/ext/integer/digits.rb
81
+ - lib/sleeping_king_studios/ext/integer/romanize.rb
66
82
  - lib/sleeping_king_studios/ext/object/metaclass.rb
67
83
  - lib/sleeping_king_studios/ext/version.rb
68
84
  - LICENSE