Commatose 0.0.1

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.
Files changed (3) hide show
  1. data/README +133 -0
  2. data/commatose.rb +87 -0
  3. metadata +55 -0
data/README ADDED
@@ -0,0 +1,133 @@
1
+ = Commatose Documentation
2
+
3
+ Commatose makes it easy to insert commas into your numbers. It extends the Fixnum, Bignum and Float classes with a new method called "to_sc". The method name is short for "To string with comma". Using Commatose couldn't be easier. Let's take a look at a few examples.
4
+
5
+ == Fixnums
6
+
7
+ Commatose can convert fixnums like so:
8
+
9
+ require 'commatose'
10
+ puts "Example 1"
11
+ num = 1234567
12
+ puts "The number in this example is a #{num.class}."
13
+ puts "Before:\t#{num}"
14
+ puts "After:\t#{num.to_sc}"
15
+
16
+ The code above returns:
17
+
18
+ Example 1
19
+ The number in this example is a Fixnum.
20
+ Before: 1234567
21
+ After: 1,234,567
22
+
23
+ {Learn more}[link:/classes/Fixnum.html]
24
+
25
+ == Bignums
26
+
27
+ Commatose can also convert bignums:
28
+
29
+ require 'commatose'
30
+ puts "Example 2"
31
+ num = 1234567890
32
+ puts "The number in this example is a #{num.class}."
33
+ puts "Before:\t#{num}"
34
+ puts "After:\t#{num.to_sc}"
35
+
36
+ The code above returns:
37
+
38
+ Example 2
39
+ The number in this example is a Bignum.
40
+ Before: 1234567890
41
+ After: 1,234,567,890
42
+
43
+ {Learn more}[link:/classes/Bignum.html]
44
+
45
+ == Floats
46
+
47
+ Commatose also handles floats:
48
+
49
+ require 'commatose'
50
+ puts "Example 3"
51
+ num = 1234567.89
52
+ puts "The number in this example is a #{num.class}."
53
+ puts "Before:\t#{num}"
54
+ puts "After:\t#{num.to_sc}"
55
+
56
+ The code above returns:
57
+
58
+ Example 3
59
+ The number in this example is a Float.
60
+ Before: 1234567.89
61
+ After: 1,234,567.89
62
+
63
+ {Learn more}[link:/classes/Float.html]
64
+
65
+ == Custom Delimiters
66
+
67
+ Commatose also supports using a custom separator. Here's a fixnum that's split by underscores instead of commas.
68
+
69
+ require 'commatose'
70
+ puts "Example 4"
71
+ num = 1234567
72
+ puts "The number in this example is a #{num.class}."
73
+ puts "Before:\t#{num}"
74
+ puts "After:\t#{num.to_sc("_")}"
75
+
76
+ The code above returns:
77
+
78
+ Example 4
79
+ The number in this example is a Fixnum.
80
+ Before: 1234567
81
+ After: 1_234_567
82
+
83
+ Commatose can use any string as a separator, including the infamous "Chunky bacon".
84
+
85
+ require 'commatose'
86
+ puts "Example 5"
87
+ num = 1234567
88
+ puts "The number in this example is a #{num.class}."
89
+ puts "Before:\t#{num}"
90
+ puts "After:\t#{num.to_sc(" Chunky bacon! ")}"
91
+
92
+ The code above returns:
93
+
94
+ Example 5
95
+ The number in this example is a Fixnum.
96
+ Before: 1234567
97
+ After: 1 Chunky bacon! 234 Chunky bacon! 567
98
+
99
+ == Numbers per Section
100
+
101
+ Commatose allows you to change the number of digits between separators.
102
+
103
+ require 'commatose'
104
+ puts "Example 6"
105
+ num = 1234567
106
+ puts "The number in this example is a #{num.class}."
107
+ puts "Before:\t#{num}"
108
+ puts "After:\t#{num.to_sc(",", 2)}"
109
+
110
+ The code above returns:
111
+
112
+ Example 6
113
+ The number in this example is a Fixnum.
114
+ Before: 1234567
115
+ After: 1,23,45,67
116
+
117
+ == Post Decimal Commatization
118
+
119
+ By default, Commatose doesn't split numbers in a float that occur after the decimal point. You can force it to split after the decimal by specifying "true" as the second argument. Note that this only applies to a float.
120
+
121
+ require 'commatose'
122
+ puts "Example 7"
123
+ num = 1234.56789
124
+ puts "The number in this example is a #{num.class}."
125
+ puts "Before:\t#{num}"
126
+ puts "After:\t#{num.to_sc(",", 3, true)}"
127
+
128
+ The code above returns:
129
+
130
+ Example 7
131
+ The number in this example is a Float.
132
+ Before: 1234.56789
133
+ After: 1,234.56,789
@@ -0,0 +1,87 @@
1
+ #= Commatose
2
+ #Commatose makes it easy to insert commas into your Fixnums, Bignums and Floats. See below for more information.
3
+ class Fixnum
4
+ #Converts the number to a string and splits it by commas.
5
+ #
6
+ #Example:
7
+ # 1234567.to_sc
8
+ # >1,234,567
9
+ #
10
+ #You can change the delimiter by giving a string as an argument.
11
+ #
12
+ #Example:
13
+ # 1234567.to_sc("_")
14
+ # >1_234_567
15
+ #
16
+ #Commatose can also change the number of digits between seperators.
17
+ #
18
+ #Example:
19
+ # 1234567.to_sc(",", 2)
20
+ # >1,23,45,67
21
+ def to_sc(delimiter = ",", numbers_per_section = 3)
22
+ self.to_s.gsub(/(\d)(?=\d{#{numbers_per_section}}+(\.\d*)?$)/, '\1' + delimiter)
23
+ end
24
+ end
25
+
26
+ #= Commatose
27
+ #Commatose makes it easy to insert commas into your Fixnums, Bignums and Floats. See below for more information.
28
+ class Bignum
29
+ #Converts the number to a string and splits it by commas.
30
+ #
31
+ #Example:
32
+ # 1234567890.to_sc
33
+ # >1,234,567,890
34
+ #
35
+ #You can change the delimiter by giving a string as an argument.
36
+ #
37
+ #Example:
38
+ # 1234567890.to_sc("_")
39
+ # >1_234_567_890
40
+ #
41
+ #Commatose can also change the number of digits between seperators.
42
+ #
43
+ #Example:
44
+ # 1234567890.to_sc(",", 2)
45
+ # >12,34,56,78,90
46
+ def to_sc(delimiter = ",", numbers_per_section = 3)
47
+ self.to_s.gsub(/(\d)(?=\d{#{numbers_per_section}}+(\.\d*)?$)/, '\1' + delimiter)
48
+ end
49
+ end
50
+
51
+ #= Commatose
52
+ #Commatose makes it easy to insert commas into your Fixnums, Bignums and Floats. See below for more information.
53
+ class Float
54
+ #Converts the number to a string and splits it by commas.
55
+ #
56
+ #Example:
57
+ # 12345.67.to_sc
58
+ # >12,345.67
59
+ #
60
+ #You can change the delimiter by giving a string as an argument.
61
+ #
62
+ #Example:
63
+ # 12345,67.to_sc("_")
64
+ # >12_345.67
65
+ #
66
+ #Commatose can also change the number of digits between seperators.
67
+ #
68
+ #Example:
69
+ # 12345.67.to_sc(",", 2)
70
+ # >1,23,45.67
71
+ #
72
+ #Commatose normally doesn't split any numbers after a decimal point. You can override that behavior by making the second argument true.
73
+ #
74
+ #Example:
75
+ # 1234.56789.to_sc
76
+ # >1,234.56789
77
+ #
78
+ # 1234.56789.to_sc(",", 3, true)
79
+ # >1,234.56,789
80
+ def to_sc(delimiter = ",", numbers_per_section = 3, after_decimal = false)
81
+ if after_decimal then
82
+ self.to_s.gsub(/(\d)(?=\d{#{numbers_per_section}}+(\.\d*)?$)/, '\1' + delimiter)
83
+ else
84
+ self.to_s.split(".")[0].gsub(/(\d)(?=\d{#{numbers_per_section}}+(\.\d*)?$)/, '\1' + delimiter) + "." + self.to_s.split(".")[1]
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Commatose
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Acts as Buffoon (A.K.A. Michael Tomer)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-03 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: Michael.Tomer@fnis.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - commatose.rb
26
+ - README
27
+ has_rdoc: true
28
+ homepage: http://actsasbuffoon.wordpress.com
29
+ post_install_message:
30
+ rdoc_options:
31
+ - --main
32
+ - README
33
+ require_paths:
34
+ - .
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Commatose makes it easy to split your numbers by commas.
54
+ test_files: []
55
+