mug 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3ad7fb03114e7062b93539759918f1ebbe542a9
4
- data.tar.gz: 35ffe2f0573509eced89ea9e2b73efb991cbd619
3
+ metadata.gz: 5c132b6a79488019536300de12dc918fe41191b9
4
+ data.tar.gz: 94c1e037fe68407949e3c3bcd0d4274c5cf8eb5d
5
5
  SHA512:
6
- metadata.gz: be694ddef15890b59a471dcea87d24f1b9df9da15c89d0d69c4c1c3786bd3f3c0bff56b876b06ecad0e4c439552610c81d2be46cd527a494b16749ffb7ff7510
7
- data.tar.gz: 524a47ce38c90eabea52a6659154cbe08869a5f59b1ba6230f0b17d48c0fd9e1f6ccb5739d95fd6e8edd5f0227365644a07647039691fc072c01a311077c7ba8
6
+ metadata.gz: dfc72b0fa90e4b518499912838f5619ae95380a8a7b18194e00a44bf788b0f24ed3cc08ce3ae218b3943a18616a5e2d9f82db15247cfab7abd44ccc12da13111
7
+ data.tar.gz: 1b0c0f63d50b07197f1db75ee2f849f08c1264bc7add2a02056b4ecd1bae619f95680051b254093ae45fb2de3f1a475e1ed204490ad581edf5c1a674a3bb260b
data/lib/mug.rb CHANGED
@@ -1,10 +1,12 @@
1
1
 
2
2
  require_relative 'mug/bool'
3
- require_relative 'mug/hashmap'
4
3
  require_relative 'mug/fragile-method-chain'
4
+ require_relative 'mug/hashmap'
5
+ require_relative 'mug/hashop'
5
6
  require_relative 'mug/iterator/for'
6
7
  require_relative 'mug/iterator/method'
7
8
  require_relative 'mug/maybe'
8
9
  require_relative 'mug/self'
10
+ require_relative 'mug/tau'
9
11
  require_relative 'mug/to_h'
10
12
 
data/lib/mug/hashop.rb ADDED
@@ -0,0 +1,39 @@
1
+
2
+ class Hash
3
+
4
+ #
5
+ # Returns a new Hash, whose value is the same as this
6
+ # one, with any extras in +o+ added in.
7
+ #
8
+ # Useful for default options.
9
+ #
10
+ # Example:
11
+ # opts = {:a => 1, :b => 2 }
12
+ # dflt = {:a => 0, :x => 9 }
13
+ # opts |= dflt # => opts = {:a=>1, :b=>2, :x=>9}
14
+ #
15
+ def | o
16
+ o.merge self
17
+ end
18
+
19
+ #
20
+ # Appends stuff to the hash.
21
+ #
22
+ # If +o+ is a Hash, this is identical to calling #merge!
23
+ # If +o+ is an Array with two elements, it is interpreted as [key,value]
24
+ # If +o+ can be converted to a hash with #to_h, this is identical to calling #merge!
25
+ # Otherwise an ArgumentError is raised.
26
+ #
27
+ def << o
28
+ if o.respond_to? :to_hash
29
+ merge! o.to_hash
30
+ elsif o.respond_to?(:to_a) && (a = o.to_a) && a.length == 2
31
+ store a[0], a[1]
32
+ elsif o.respond_to? :to_h
33
+ merge! o.to_h
34
+ else
35
+ raise ArgumentError, "#{o.class.name} is not a Hash"
36
+ end
37
+ end
38
+ end
39
+
data/lib/mug/tau.rb ADDED
@@ -0,0 +1,44 @@
1
+
2
+ module Math
3
+ # The true circle constant.
4
+ # The ratio of a circle's circumference to its radius.
5
+ TAU = PI * 2.0
6
+ end
7
+
8
+ module BigMath
9
+ ##
10
+ # Computes the value of tau to the specific number of digits of precision.
11
+ #
12
+ # @param [Integer] prec the number of decimal digits of precision in the computed value.
13
+ # @return [BigDecimal] the computed value
14
+ # @raise [ArgumentError] if +prec+ is not positive
15
+ #
16
+ # @example
17
+ # require 'bigdecimal'
18
+ # require 'bigdecimal/math'
19
+ # include BigMath
20
+ #
21
+ # puts TAU(150)
22
+ #
23
+ def TAU(prec)
24
+ raise ArgumentError, 'Zero or negative argument for TAU' if prec <= 0
25
+ PI(prec) * BigDecimal('2')
26
+ end
27
+ end
28
+
29
+ =begin
30
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
31
+
32
+ Permission to use, copy, modify, and/or distribute this software for any
33
+ purpose with or without fee is hereby granted, provided that the above
34
+ copyright notice and this permission notice appear in all copies.
35
+
36
+ THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
37
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
38
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
39
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
41
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
42
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43
+ =end
44
+
@@ -0,0 +1,50 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ $coercable = Object.new
5
+ def $coercable.to_h
6
+ {:a=>0, :z=>99}
7
+ end
8
+
9
+ require_relative '../lib/mug/hashop'
10
+ class Test_hashop < Test::Unit::TestCase
11
+ def test_hash_or
12
+ h = { :a => 1, :b => 2 }
13
+ [
14
+ [{}, h, h],
15
+ [{:c=>3}, h, {:a=>1, :b=>2, :c=>3}],
16
+ [{:a=>0, :b=>0}, h, {:a=>0, :b=>0}],
17
+ [{:a=>0, :c=>3}, h, {:a=>0, :b=>2, :c=>3}],
18
+ [h, {}, h],
19
+ ].each do |a, b, x|
20
+ assert_equal( a|b, x )
21
+ end
22
+ end
23
+ def test_hash_poke
24
+ h = {}
25
+
26
+ # Regular Hash poking
27
+ assert_nothing_raised { h << {:a=>1} }
28
+ assert_equal h, {:a=>1}
29
+ assert_nothing_raised { h << {:b=>2} }
30
+ assert_equal h, {:a=>1,:b=>2}
31
+ assert_nothing_raised { h << {:a=>3} }
32
+ assert_equal h, {:a=>3,:b=>2}
33
+ assert_nothing_raised { h << {:a=>1,:c=>3} }
34
+ assert_equal h, {:a=>1,:b=>2,:c=>3}
35
+
36
+ # Two-element Array poking
37
+ assert_nothing_raised { h << [:d,1] }
38
+ assert_equal h, {:a=>1,:b=>2,:c=>3,:d=>1}
39
+ assert_nothing_raised { h << [:d,4] }
40
+ assert_equal h, {:a=>1,:b=>2,:c=>3,:d=>4}
41
+
42
+ # Objects with .to_h
43
+ assert_nothing_raised { h << $coercable }
44
+ assert_equal h, {:a=>0,:b=>2,:c=>3,:d=>4,:z=>99}
45
+
46
+ # Failure
47
+ assert_raise(ArgumentError) { h << Object.new }
48
+ end
49
+ end
50
+
data/test/test-tau.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'bigdecimal'
3
+ require 'bigdecimal/math'
4
+
5
+ $VERBOSE = true
6
+ require_relative '../lib/mug/tau'
7
+ class Test_tau < Test::Unit::TestCase
8
+ def test_math_tau
9
+ # 6.283185307179586..
10
+ assert_equal(Math::TAU, Math::PI*2.0)
11
+ end
12
+
13
+ include BigMath
14
+ def test_TAU
15
+ assert_equal(TAU(1).round(1).to_s('F'), '6.3')
16
+ assert_equal(TAU(100).truncate(100).to_s('F'), '6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696506842341359')
17
+ assert_raise(ArgumentError) { TAU(0) }
18
+ assert_raise(ArgumentError) { TAU(-3) }
19
+ assert_raise(ArgumentError) { TAU('barf') }
20
+ end
21
+ end
22
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-25 00:00:00.000000000 Z
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  == MUG: Matty's Ultimate Gem
@@ -22,15 +22,18 @@ extra_rdoc_files: []
22
22
  files:
23
23
  - lib/mug.rb
24
24
  - lib/mug/hashmap.rb
25
+ - lib/mug/tau.rb
25
26
  - lib/mug/to_h.rb
26
27
  - lib/mug/fragile-method-chain.rb
27
28
  - lib/mug/self.rb
28
29
  - lib/mug/iterator.rb
30
+ - lib/mug/hashop.rb
29
31
  - lib/mug/iterator/for.rb
30
32
  - lib/mug/iterator/method.rb
31
33
  - lib/mug/maybe.rb
32
34
  - lib/mug/iterator_c.rb
33
35
  - lib/mug/bool.rb
36
+ - test/test-tau.rb
34
37
  - test/test-self.rb
35
38
  - test/test-bool.rb
36
39
  - test/test-to_h.rb
@@ -39,6 +42,7 @@ files:
39
42
  - test/test-iterator-for.rb
40
43
  - test/test-maybe.rb
41
44
  - test/test-hashmap.rb
45
+ - test/test-hashop.rb
42
46
  homepage: http://phluid61.github.com/mug
43
47
  licenses:
44
48
  - ISC License
@@ -59,11 +63,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
63
  version: '0'
60
64
  requirements: []
61
65
  rubyforge_project:
62
- rubygems_version: 2.0.2
66
+ rubygems_version: 2.0.3
63
67
  signing_key:
64
68
  specification_version: 4
65
69
  summary: 'MUG: Matty''s Ultimate Gem'
66
70
  test_files:
71
+ - test/test-tau.rb
67
72
  - test/test-self.rb
68
73
  - test/test-bool.rb
69
74
  - test/test-to_h.rb
@@ -72,3 +77,4 @@ test_files:
72
77
  - test/test-iterator-for.rb
73
78
  - test/test-maybe.rb
74
79
  - test/test-hashmap.rb
80
+ - test/test-hashop.rb