numscaler 0.0.4 → 0.0.5

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: 331c09c6168e5869643abc1cb61b2add72653db9
4
- data.tar.gz: 160bedb49e76c394081108b17d6b8b5a39305f34
3
+ metadata.gz: 7be33f9f41d6a311789c24901c46e282f44577ba
4
+ data.tar.gz: 14dfbe2d52b7a4535ebc56b1533c0b4fc8877e39
5
5
  SHA512:
6
- metadata.gz: a586ebb9b32110e06ad3f10ab6618381c946c10416801dc62057f9aef4ed4e530f960a31328a11bbc2370d6e31b3278e8714fd14a79848096bcbd2f1325c3771
7
- data.tar.gz: 3af7b7301e3068832b98d05fe225507b601cff23718592e1fe07e17b4eadd6a198f228fb9c9f2bb5cfc7631308fa5f97b168078d3e7d6f0d28a5b530f80b1564
6
+ metadata.gz: 7fc1433ded6360e54fb7555c19928baa30e89a117b79d31b612ffe60100a919610cc0915ccf8b4b2940c80dd13da890846cbdfcabf87056b68417dac2ba3e23b
7
+ data.tar.gz: c09f9d90aac10634875d0318ddb3737cab03fbbfea976d918289c2006f81889d33a916e310a5d0ec48ca75ed152951d7827919ee2a66969b0cf4441715e25092
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  doc/
2
2
  pkg/
3
3
  .yardoc/
4
+ .rspec
data/README.md CHANGED
@@ -29,73 +29,135 @@ and therefore that is the default rounding.
29
29
 
30
30
  There is a pretty brute-force test suite. Should work with any Ruby version.
31
31
 
32
+ ## Applications
33
+
34
+ * Scaling when graphing
35
+ ...from `0.0..1.0` to `0..(height-1)`
36
+ * Bounded unit conversion
37
+ ...from Celsius to Fahrenheit, Centimeters to Inches
38
+ * Circular unit conversion
39
+ ...from degrees to radians
40
+ * Value to index mapping
41
+ ...from `0.0..1.0` to 'very bad' - 'very good'
42
+
32
43
  ## Examples
33
44
 
34
- Install and run pry/irb:
45
+ Install:
35
46
 
36
47
  $ gem install numscaler
37
- $ pry
38
-
39
- Simple Integer - Integer conversion:
40
-
41
- [1] pry(main)> require 'numscaler'
42
- [2] pry(main)> iti = NumScaler.new(0..15, 0..256)
43
- [3] pry(main)> iti.from(0)
44
- => 0
45
- [4] pry(main)> iti.from(7)
46
- => 119
47
- [5] pry(main)> iti.from(8)
48
- => 137
49
- [6] pry(main)> iti.to(128)
50
- => 8
51
- [7] pry(main)> iti.to(255)
52
- => 15
53
- [8] pry(main)> iti.from(-1)
54
- ArgumentError: Number out of range
55
-
56
- Integer - Float conversion:
57
-
58
- [9] pry(main)> itf = NumScaler.new(0..7, 0.0..16.0)
59
- [10] pry(main)> itf.from(2)
60
- => 4.57142857142857
61
- [11] pry(main)> itf.from(7)
62
- => 16.0
63
- [12] pry(main)> itf.to(4)
64
- => 2
65
- [13] pry(main)> itf.to(4.234)
66
- => 2
67
-
68
- Float - Float conversion:
69
-
70
- [14] pry(main)> ftf = NumScaler.new(-2.5..2.5, -5.0..5.0)
71
- [15] pry(main)> ftf.from(-2.0)
72
- => -4.0
73
- [16] pry(main)> ftf.to(-4.0)
74
- => -2.0
75
- [17] pry(main)> # deg - rad conversion
76
- [18] pry(main)> ftf = NumScaler.new(0.0..180.0, 0.0..Math::PI)
77
- [19] pry(main)> ftf.from(45.0)
78
- => 0.78539816339745
79
- [20] pry(main)> ftf.to(1.57079633)
80
- => 90.0000001836389
81
-
82
- Clamping examples:
83
-
84
- [21] pry(main)> ftf = NumScaler.new(0.0..4.0, -2.5..2.5, :clamp)
85
- [22] pry(main)> ftf.from(-1.0)
86
- => -2.5
87
- [23] pry(main)> ftf.from(5.0)
88
- => 2.5
89
- [24] pry(main)> ftf.to(15.0)
90
- => 4.0
48
+
49
+ From the `examples/` directory, first some graphing:
50
+
51
+ require 'numscaler'
52
+
53
+ s1 = NumScaler.new(0..64, 0.0..Math::PI*2.0)
54
+ s2 = NumScaler.new(-1.0..1.0, 0..9)
55
+
56
+ graph = (0..64).to_a.collect do |e|
57
+ i = s2.from(Math.sin(s1.from(e)))
58
+ a = [' '] * 10
59
+ a[i] = '#'
60
+ a
61
+ end
62
+
63
+ puts graph.transpose.collect {|e| e.join }
64
+
65
+ Running it will produce a lovely:
66
+
67
+ #########
68
+ #### ####
69
+ ### ###
70
+ ## ##
71
+ ## ##
72
+ ### ### #
73
+ ## ##
74
+ ### ###
75
+ #### ####
76
+ #########
77
+
78
+ You can also use it for unit conversion if you wish, like so:
79
+
80
+ require 'numscaler'
81
+
82
+ distance = NumScaler.new(0.0..100.0, 0.0..39.3701)
83
+ temperature = NumScaler.new(-30.0..120.0, -22.0..248.0)
84
+ angle = NumScaler.new(0.0..90.0, 0.0..Math::PI/2.0)
85
+
86
+ puts 'Distance:'
87
+ [
88
+ ['9 mm ammo', 0.9],
89
+ ['max pin distance in an europlug', 1.86],
90
+ ['average baguette length', 65.0],
91
+ ].each do |label, cm|
92
+ print "#{label} (#{cm} cm) is ".rjust(50)
93
+ puts distance.from(cm).to_s + ' inch'
94
+ end
95
+
96
+ puts "\nTemperature:"
97
+ [
98
+ ['siberian cold', -25.0],
99
+ ['minimal workplace temp', 18.0],
100
+ ['usually comfortable', 25.0],
101
+ ['Polish summer', 35.0],
102
+ ].each do |label, cent|
103
+ print "#{label} (#{cent} Celsius) is ".rjust(50)
104
+ puts temperature.from(cent).to_s + ' Fahrenheit'
105
+ end
106
+
107
+ puts "\nAngle:"
108
+ [
109
+ ['human FOV blind spot width', 5.5],
110
+ ['decent slope', 23.0],
111
+ ['in the corner', 90.0],
112
+ ].each do |label, deg|
113
+ print "#{label} (#{deg} degrees) is ".rjust(50)
114
+ puts angle.from(deg).to_s + ' radians'
115
+ end
116
+
117
+ Which will produce:
118
+
119
+ Distance:
120
+ 9 mm ammo (0.9 cm) is 0.3543309 inch
121
+ max pin distance in a europlug (1.86 cm) is 0.73228386 inch
122
+ average baguette length (65.0 cm) is 25.590565 inch
123
+
124
+ Temperature:
125
+ siberian cold (-25.0 Celsius) is -13.0 Fahrenheit
126
+ minimal workplace temp (18.0 Celsius) is 64.4 Fahrenheit
127
+ usually comfortable (25.0 Celsius) is 77.0 Fahrenheit
128
+ Polish summer (35.0 Celsius) is 95.0 Fahrenheit
129
+
130
+ Angle:
131
+ human FOV blind spot width (5.5 degrees) is 0.09599310885969 radians
132
+ decent slope (23.0 degrees) is 0.4014257279587 radians
133
+ in the corner (90.0 degrees) is 1.5707963267949 radians
134
+
135
+ You can also do some esoteric stuff, like:
136
+
137
+ require 'numscaler'
138
+
139
+ palette = ' ,-\'"\\O/"\'-. '.split('')
140
+ s = NumScaler.new(-1.0..1.0, 0..(palette.length - 1), :mode => :cycle)
141
+ c = NumScaler.new(1..32, 0.0..Math::PI*2.0, :mode => :cycle)
91
142
 
92
- [25] pry(main)> ftf = NumScaler.new(0.0..4.0, -2.5..2.5, :cycle)
93
- [26] pry(main)> ftf.from(-1.0)
94
- => 1.25
95
- [27] pry(main)> ftf.from(5.0)
96
- => -1.25
97
- [28] pry(main)> ftf.to(15.0)
98
- => 2.0
143
+ 1.upto(256) do |o|
144
+ puts "\e[H\e[2J"
145
+ 1.upto(32) do |y|
146
+ 1.upto(64) do |x|
147
+ print palette[s.from(
148
+ Math.tan(c.from(o)) *\
149
+ (Math.sin(c.from(x)) +\
150
+ Math.cos(c.from(y)))
151
+ )]
152
+ end
153
+ print "\n"
154
+ end
155
+ print "\n"
156
+ sleep(0.5)
157
+ end
158
+
159
+ For this you'd have to run it in a terminal (and it will probably not work
160
+ as intended on windows).
99
161
 
100
162
  ## Copyright
101
163
 
@@ -0,0 +1,21 @@
1
+ require 'numscaler'
2
+
3
+ palette = ' ,-\'"\\O/"\'-. '.split('')
4
+ s = NumScaler.new(-1.0..1.0, 0..(palette.length - 1), :mode => :cycle)
5
+ c = NumScaler.new(1..32, 0.0..Math::PI*2.0, :mode => :cycle)
6
+
7
+ 1.upto(256) do |o|
8
+ puts "\e[H\e[2J"
9
+ 1.upto(32) do |y|
10
+ 1.upto(64) do |x|
11
+ print palette[s.from(
12
+ Math.tan(c.from(o)) *\
13
+ (Math.sin(c.from(x)) +\
14
+ Math.cos(c.from(y)))
15
+ )]
16
+ end
17
+ print "\n"
18
+ end
19
+ print "\n"
20
+ sleep(0.5)
21
+ end
@@ -0,0 +1,13 @@
1
+ require 'numscaler'
2
+
3
+ s1 = NumScaler.new(0..64, 0.0..Math::PI*2.0)
4
+ s2 = NumScaler.new(-1.0..1.0, 0..9)
5
+
6
+ graph = (0..64).to_a.collect do |e|
7
+ i = s2.from(Math.sin(s1.from(e)))
8
+ a = [' '] * 10
9
+ a[i] = '#'
10
+ a
11
+ end
12
+
13
+ puts graph.transpose.collect {|e| e.join }
@@ -0,0 +1,36 @@
1
+ require 'numscaler'
2
+
3
+ distance = NumScaler.new(0.0..100.0, 0.0..39.3701)
4
+ temperature = NumScaler.new(-30.0..120.0, -22.0..248.0)
5
+ angle = NumScaler.new(0.0..90.0, 0.0..Math::PI/2.0)
6
+
7
+ puts 'Distance:'
8
+ [
9
+ ['9 mm ammo', 0.9],
10
+ ['max pin distance in an europlug', 1.86],
11
+ ['average baguette length', 65.0],
12
+ ].each do |label, cm|
13
+ print "#{label} (#{cm} cm) is ".rjust(50)
14
+ puts distance.from(cm).to_s + ' inch'
15
+ end
16
+
17
+ puts "\nTemperature:"
18
+ [
19
+ ['siberian cold', -25.0],
20
+ ['minimal workplace temp', 18.0],
21
+ ['usually comfortable', 25.0],
22
+ ['Polish summer', 35.0],
23
+ ].each do |label, cent|
24
+ print "#{label} (#{cent} Celsius) is ".rjust(50)
25
+ puts temperature.from(cent).to_s + ' Fahrenheit'
26
+ end
27
+
28
+ puts "\nAngle:"
29
+ [
30
+ ['human FOV blind spot width', 5.5],
31
+ ['decent slope', 23.0],
32
+ ['in the corner', 90.0],
33
+ ].each do |label, deg|
34
+ print "#{label} (#{deg} degrees) is ".rjust(50)
35
+ puts angle.from(deg).to_s + ' radians'
36
+ end
@@ -3,30 +3,34 @@
3
3
  # See {file:README.md} for practical examples.
4
4
  class NumScaler
5
5
  # NumScaler version string
6
- VERSION = '0.0.4'
6
+ VERSION = '0.0.5'
7
7
  # Epsilon defines rounding precision
8
8
  EPSILON = 14
9
9
  # Available clamping modes
10
10
  MODES = [:strict, :cycle, :clamp]
11
11
 
12
+ # Additional `options`:
13
+ #
14
+ # * `:mode` - specify clamping mode (*default: `:strict`*)
15
+ # * `:precision` - specify Float rounding (*default: `EPSILON`*)
16
+ #
17
+ # Precision defines number of significant decimal digits for rounding.
18
+ #
12
19
  # Current clamping modes:
13
20
  #
14
21
  # * `:strict` - raise ArgumentError for out-of-range number (*default*)
15
22
  # * `:clamp` - clamp number to source range
16
23
  # * `:cycle` - treat range as a circle of values
17
24
  #
18
- # Precision defines number of significant decimal digits for rounding.
19
- #
20
25
  # @param from [Range] source range
21
26
  # @param to [Range] target range
22
- # @param mode [Symbol] clamping mode
23
- # @param precision [Float] rounding precision
24
- def initialize(from, to, mode = MODES.first, precision = EPSILON)
25
- raise ArgumentError, 'Unknown mode' unless MODES.member? mode
26
- raise ArgumentError, 'Precision out of ranger' unless precision > 0
27
+ # @param options [Hash] additional options
28
+ def initialize(from, to, options = {})
29
+ @mode = options[:mode] || MODES.first
30
+ @prec = options[:precision] || EPSILON
27
31
 
28
- @mode = mode
29
- @prec = precision
32
+ raise ArgumentError, 'Unknown mode' unless MODES.member? @mode
33
+ raise ArgumentError, 'Precision out of range' unless @prec > 0
30
34
 
31
35
  @src = { :orig => from.min,
32
36
  :range => from.max.to_f - from.min.to_f,
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^spec/})
19
19
  gem.require_paths = ['lib']
20
20
 
21
+ gem.required_ruby_version = '>= 1.8.7'
22
+
21
23
  gem.add_development_dependency 'rspec', '~> 2.4'
22
24
  gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
23
25
  gem.add_development_dependency 'yard', '~> 0.8'
@@ -3,40 +3,49 @@
3
3
  require 'rspec'
4
4
  require 'numscaler'
5
5
 
6
+ RSpec.configure do |c|
7
+ c.expect_with :rspec do |cc|
8
+ cc.syntax = :expect
9
+ end
10
+ end
11
+
6
12
  describe NumScaler do
7
13
  it 'should have a VERSION constant' do
8
- NumScaler::VERSION.should_not be_empty
14
+ expect(NumScaler::VERSION).to_not be_empty
9
15
  end
10
16
 
11
17
  it 'should have an EPSILON constant' do
12
- NumScaler::EPSILON.should > 0
18
+ expect(NumScaler::EPSILON).to be > 0
13
19
  end
14
20
 
15
21
  it 'should define clamping MODES' do
16
- NumScaler::MODES.should_not be_empty
22
+ expect(NumScaler::MODES).to_not be_empty
17
23
  end
18
24
 
19
25
  it 'should check arguments' do
20
- lambda {
21
- NumScaler.new(1..8, 16..32, :nonext)
22
- }.should raise_error(ArgumentError)
26
+ expect {
27
+ NumScaler.new(1..8, 16..32, :mode => :nonext)
28
+ }.to raise_error(ArgumentError)
29
+ expect {
30
+ NumScaler.new(1..8, 16..32, :precision => 0)
31
+ }.to raise_error(ArgumentError)
23
32
  end
24
33
 
25
34
  context 'should work with Integer <=> Integer ranges and' do
26
35
  it 'should convert from' do
27
- scaler = NumScaler.new(1..8, 16..32)
36
+ s = NumScaler.new(1..8, 16..32)
28
37
 
29
- scaler.from(1).should eq 16
30
- scaler.from(4).should eq 23
31
- scaler.from(8).should eq 32
38
+ expect(s.from(1)).to eql 16
39
+ expect(s.from(4)).to eql 23
40
+ expect(s.from(8)).to eql 32
32
41
  end
33
42
 
34
43
  it 'should convert to' do
35
- scaler = NumScaler.new(1..8, -32..32)
44
+ s = NumScaler.new(1..8, -32..32)
36
45
 
37
- scaler.to(-32).should eq 1
38
- scaler.to( 0).should eq 5
39
- scaler.to( 32).should eq 8
46
+ expect(s.to(-32)).to eql 1
47
+ expect(s.to( 0)).to eql 5
48
+ expect(s.to( 32)).to eql 8
40
49
  end
41
50
  end
42
51
 
@@ -46,27 +55,27 @@ describe NumScaler do
46
55
  end
47
56
 
48
57
  it 'should convert from' do
49
- scaler = NumScaler.new(0..15, 0.0..Math::PI)
50
- scaler.from( 0 ).should eq 0.0
51
- scaler.from( 7.5).should eq (Math::PI/2.0).round(@prec)
52
- scaler.from(15 ).should eq Math::PI.round(@prec)
53
-
54
- scaler = NumScaler.new(-15..15, 0.0..Math::PI)
55
- scaler.from(-15).should eq 0.0
56
- scaler.from( 0).should eq (Math::PI/2.0).round(@prec)
57
- scaler.from( 15).should eq Math::PI.round(@prec)
58
+ s = NumScaler.new(0..15, 0.0..Math::PI)
59
+ expect(s.from( 0 )).to eql 0.0
60
+ expect(s.from( 7.5)).to eql (Math::PI/2.0).round(@prec)
61
+ expect(s.from(15 )).to eql Math::PI.round(@prec)
62
+
63
+ s = NumScaler.new(-15..15, 0.0..Math::PI)
64
+ expect(s.from(-15)).to eql 0.0
65
+ expect(s.from( 0)).to eql (Math::PI/2.0).round(@prec)
66
+ expect(s.from( 15)).to eql Math::PI.round(@prec)
58
67
  end
59
68
 
60
69
  it 'should convert to' do
61
- scaler = NumScaler.new(0..15, 0.0..Math::PI)
62
- scaler.to(0.0).should eq 0
63
- scaler.to(Math::PI/2.0).should eq 7
64
- scaler.to(Math::PI).should eq 15
65
-
66
- scaler = NumScaler.new(-15..15, 0.0..Math::PI)
67
- scaler.to(0.0).should eq -15
68
- scaler.to(Math::PI/2.0).should eq 0
69
- scaler.to(Math::PI).should eq 15
70
+ s = NumScaler.new(0..15, 0.0..Math::PI)
71
+ expect(s.to(0.0)).to eql 0
72
+ expect(s.to(Math::PI/2.0)).to eql 7
73
+ expect(s.to(Math::PI)).to eql 15
74
+
75
+ s = NumScaler.new(-15..15, 0.0..Math::PI)
76
+ expect(s.to(0.0)).to eql -15
77
+ expect(s.to(Math::PI/2.0)).to eql 0
78
+ expect(s.to(Math::PI)).to eql 15
70
79
  end
71
80
  end
72
81
 
@@ -76,108 +85,108 @@ describe NumScaler do
76
85
  end
77
86
 
78
87
  it 'should convert from' do
79
- scaler = NumScaler.new(0.0..256.0, 0.0..Math::PI)
80
- scaler.from( 0.0).should eq 0.0
81
- scaler.from( 32.0).should eq (Math::PI/8.0).round(@prec)
82
- scaler.from( 64.0).should eq (Math::PI/4.0).round(@prec)
83
- scaler.from(128.0).should eq (Math::PI/2.0).round(@prec)
84
- scaler.from(256.0).should eq Math::PI.round(@prec)
85
-
86
- scaler = NumScaler.new(0.0..256.0, -Math::PI..Math::PI)
87
- scaler.from( 0.0).should eq -Math::PI.round(@prec)
88
- scaler.from( 64.0).should eq (-Math::PI/2.0).round(@prec)
89
- scaler.from(128.0).should eq 0.0
90
- scaler.from(192.0).should eq (Math::PI/2.0).round(@prec)
91
- scaler.from(256.0).should eq Math::PI.round(@prec)
88
+ s = NumScaler.new(0.0..256.0, 0.0..Math::PI)
89
+ expect(s.from( 0.0)).to eql 0.0
90
+ expect(s.from( 32.0)).to eql (Math::PI/8.0).round(@prec)
91
+ expect(s.from( 64.0)).to eql (Math::PI/4.0).round(@prec)
92
+ expect(s.from(128.0)).to eql (Math::PI/2.0).round(@prec)
93
+ expect(s.from(256.0)).to eql Math::PI.round(@prec)
94
+
95
+ s = NumScaler.new(0.0..256.0, -Math::PI..Math::PI)
96
+ expect(s.from( 0.0)).to eql -Math::PI.round(@prec)
97
+ expect(s.from( 64.0)).to eql (-Math::PI/2.0).round(@prec)
98
+ expect(s.from(128.0)).to eql 0.0
99
+ expect(s.from(192.0)).to eql (Math::PI/2.0).round(@prec)
100
+ expect(s.from(256.0)).to eql Math::PI.round(@prec)
92
101
  end
93
102
 
94
103
  it 'should convert to' do
95
- scaler = NumScaler.new(0.0..256.0, 0.0..Math::PI)
96
- scaler.to(0.0).should eq 0.0
97
- scaler.to(Math::PI/8.0).should eq 32.0
98
- scaler.to(Math::PI/4.0).should eq 64.0
99
- scaler.to(Math::PI/2.0).should eq 128.0
100
- scaler.to(Math::PI).should eq 256.0
101
-
102
- scaler = NumScaler.new(0.0..256.0, -Math::PI..Math::PI)
103
- scaler.to(-Math::PI).should eq 0.0
104
- scaler.to(-Math::PI/2.0).should eq 64.0
105
- scaler.to(0.0).should eq 128.0
106
- scaler.to(Math::PI/2.0).should eq 192.0
107
- scaler.to(Math::PI).should eq 256.0
104
+ s = NumScaler.new(0.0..256.0, 0.0..Math::PI)
105
+ expect(s.to(0.0)).to eql 0.0
106
+ expect(s.to(Math::PI/8.0)).to eql 32.0
107
+ expect(s.to(Math::PI/4.0)).to eql 64.0
108
+ expect(s.to(Math::PI/2.0)).to eql 128.0
109
+ expect(s.to(Math::PI)).to eql 256.0
110
+
111
+ s = NumScaler.new(0.0..256.0, -Math::PI..Math::PI)
112
+ expect(s.to(-Math::PI)).to eql 0.0
113
+ expect(s.to(-Math::PI/2.0)).to eql 64.0
114
+ expect(s.to(0.0)).to eql 128.0
115
+ expect(s.to(Math::PI/2.0)).to eql 192.0
116
+ expect(s.to(Math::PI)).to eql 256.0
108
117
  end
109
118
  end
110
119
 
111
120
  context 'should :strict clamp properly' do
112
121
  it 'with Integer ranges' do
113
- scaler = NumScaler.new(1..8, -5..5)
114
- lambda { scaler.from(-1) }.should raise_error ArgumentError
115
- lambda { scaler.from( 0) }.should raise_error ArgumentError
116
- lambda { scaler.from( 9) }.should raise_error ArgumentError
117
- lambda { scaler.to(-6) }.should raise_error ArgumentError
118
- lambda { scaler.to( 6) }.should raise_error ArgumentError
122
+ s = NumScaler.new(1..8, -5..5)
123
+ expect { s.from(-1) }.to raise_error ArgumentError
124
+ expect { s.from( 0) }.to raise_error ArgumentError
125
+ expect { s.from( 9) }.to raise_error ArgumentError
126
+ expect { s.to(-6) }.to raise_error ArgumentError
127
+ expect { s.to( 6) }.to raise_error ArgumentError
119
128
  end
120
129
 
121
130
  it 'with Float ranges' do
122
- scaler = NumScaler.new(0.5..1.25, -5.0..5.0)
123
- lambda { scaler.from(0.42 ) }.should raise_error ArgumentError
124
- lambda { scaler.from(0.499) }.should raise_error ArgumentError
125
- lambda { scaler.from(1.26 ) }.should raise_error ArgumentError
126
- lambda { scaler.from(1.3 ) }.should raise_error ArgumentError
127
- lambda { scaler.to(-5.1) }.should raise_error ArgumentError
128
- lambda { scaler.to( 5.1) }.should raise_error ArgumentError
131
+ s = NumScaler.new(0.5..1.25, -5.0..5.0)
132
+ expect { s.from(0.42 ) }.to raise_error ArgumentError
133
+ expect { s.from(0.499) }.to raise_error ArgumentError
134
+ expect { s.from(1.26 ) }.to raise_error ArgumentError
135
+ expect { s.from(1.3 ) }.to raise_error ArgumentError
136
+ expect { s.to(-5.1) }.to raise_error ArgumentError
137
+ expect { s.to( 5.1) }.to raise_error ArgumentError
129
138
  end
130
139
  end
131
140
 
132
141
  context 'should :clamp clamp properly' do
133
142
  it 'with Integer ranges' do
134
- scaler = NumScaler.new(1..8, -32..32, :clamp)
135
- scaler.from(-1).should eq -32
136
- scaler.from( 0).should eq -32
137
- scaler.from( 9).should eq 32
138
- scaler.to(-33).should eq 1
139
- scaler.to( 0).should eq 5
140
- scaler.to( 33).should eq 8
143
+ s = NumScaler.new(1..8, -32..32, :mode => :clamp)
144
+ expect(s.from(-1)).to eql -32
145
+ expect(s.from( 0)).to eql -32
146
+ expect(s.from( 9)).to eql 32
147
+ expect(s.to(-33)).to eql 1
148
+ expect(s.to( 0)).to eql 5
149
+ expect(s.to( 33)).to eql 8
141
150
  end
142
151
 
143
152
  it 'with Float ranges' do
144
- scaler = NumScaler.new(1.0..8.0, -32.0..32.0, :clamp)
145
- scaler.from(0.89).should eq -32.0
146
- scaler.from(8.01).should eq 32.0
147
- scaler.to(-32.1).should eq 1.0
148
- scaler.to( 32.1).should eq 8.0
153
+ s = NumScaler.new(1.0..8.0, -32.0..32.0, :mode => :clamp)
154
+ expect(s.from(0.89)).to eql -32.0
155
+ expect(s.from(8.01)).to eql 32.0
156
+ expect(s.to(-32.1)).to eql 1.0
157
+ expect(s.to( 32.1)).to eql 8.0
149
158
  end
150
159
  end
151
160
 
152
161
  context 'should :cycle clamp properly' do
153
162
  it 'with Integer ranges' do
154
- scaler = NumScaler.new(1..8, -8..-1, :cycle)
155
- scaler.from(-2).should eq -4
156
- scaler.from( 0).should eq -2
157
- scaler.from( 2).should eq -7
158
- scaler.from( 9).should eq -7
159
- scaler.to(-9).should eq 7
160
- scaler.to( 0).should eq 2
161
-
162
- scaler = NumScaler.new(-5..5, 20..30, :cycle)
163
- scaler.from(-8).should eq 27
164
- scaler.from(-7).should eq 28
165
- scaler.from(-6).should eq 29
166
- scaler.from( 6).should eq 21
167
- scaler.from( 7).should eq 22
168
- scaler.to(18).should eq 3
169
- scaler.to(19).should eq 4
170
- scaler.to(31).should eq -4
171
- scaler.to(41).should eq -4
163
+ s = NumScaler.new(1..8, -8..-1, :mode => :cycle)
164
+ expect(s.from(-2)).to eql -4
165
+ expect(s.from( 0)).to eql -2
166
+ expect(s.from( 2)).to eql -7
167
+ expect(s.from( 9)).to eql -7
168
+ expect(s.to(-9)).to eql 7
169
+ expect(s.to( 0)).to eql 2
170
+
171
+ s = NumScaler.new(-5..5, 20..30, :mode => :cycle)
172
+ expect(s.from(-8)).to eql 27
173
+ expect(s.from(-7)).to eql 28
174
+ expect(s.from(-6)).to eql 29
175
+ expect(s.from( 6)).to eql 21
176
+ expect(s.from( 7)).to eql 22
177
+ expect(s.to(18)).to eql 3
178
+ expect(s.to(19)).to eql 4
179
+ expect(s.to(31)).to eql -4
180
+ expect(s.to(41)).to eql -4
172
181
  end
173
182
 
174
183
  it 'with Float ranges' do
175
- scaler = NumScaler.new(-2.5..2.5, 20.0..30.0, :cycle)
176
- scaler.from(-6.0).should eq 23.0
177
- scaler.from(-2.0).should eq 21.0
178
- scaler.from( 0.0).should eq 25.0
179
- scaler.from( 3.0).should eq 21.0
180
- scaler.from( 6.0).should eq 27.0
184
+ s = NumScaler.new(-2.5..2.5, 20.0..30.0, :mode => :cycle)
185
+ expect(s.from(-6.0)).to eql 23.0
186
+ expect(s.from(-2.0)).to eql 21.0
187
+ expect(s.from( 0.0)).to eql 25.0
188
+ expect(s.from( 3.0)).to eql 21.0
189
+ expect(s.from( 6.0)).to eql 27.0
181
190
  end
182
191
  end
183
192
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numscaler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr S. Staszewski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-27 00:00:00.000000000 Z
11
+ date: 2014-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -60,11 +60,13 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - ".document"
62
62
  - ".gitignore"
63
- - ".rspec"
64
63
  - ".yardopts"
65
64
  - LICENSE.txt
66
65
  - README.md
67
66
  - Rakefile
67
+ - examples/esoteric.rb
68
+ - examples/graphing.rb
69
+ - examples/units.rb
68
70
  - lib/numscaler.rb
69
71
  - numscaler.gemspec
70
72
  - spec/numscaler_spec.rb
@@ -80,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
82
  requirements:
81
83
  - - ">="
82
84
  - !ruby/object:Gem::Version
83
- version: '0'
85
+ version: 1.8.7
84
86
  required_rubygems_version: !ruby/object:Gem::Requirement
85
87
  requirements:
86
88
  - - ">="
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --colour