shenanigans 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/doc/Array.html CHANGED
@@ -98,6 +98,8 @@
98
98
 
99
99
  <li><a href="./Array.html">Array</a>
100
100
 
101
+ <li><a href="./Hash.html">Hash</a>
102
+
101
103
  <li><a href="./Kernel.html">Kernel</a>
102
104
 
103
105
  <li><a href="./Object.html">Object</a>
@@ -196,7 +198,7 @@ shorter of the two arrays.</p>
196
198
 
197
199
  <p>For more complex combinations, a block can be provided:</p>
198
200
 
199
- <pre class="ruby">[<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>].<span class="ruby-identifier">zw</span>([<span class="ruby-value">6</span>,<span class="ruby-value">5</span>,<span class="ruby-value">4</span>]) { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>,<span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-value">3</span>*<span class="ruby-identifier">a</span><span class="ruby-operator">+</span><span class="ruby-value">2</span>*<span class="ruby-identifier">b</span> }
201
+ <pre class="ruby">[<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>].<span class="ruby-identifier">zip_with</span>([<span class="ruby-value">6</span>,<span class="ruby-value">5</span>,<span class="ruby-value">4</span>]) { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>,<span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-value">3</span>*<span class="ruby-identifier">a</span><span class="ruby-operator">+</span><span class="ruby-value">2</span>*<span class="ruby-identifier">b</span> }
200
202
  <span class="ruby-comment">#=&gt; [15, 16, 17]</span>
201
203
  </pre>
202
204
 
data/doc/Hash.html ADDED
@@ -0,0 +1,194 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
6
+
7
+ <title>class Hash - Shenanigans</title>
8
+
9
+ <link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">
10
+
11
+ <script type="text/javascript">
12
+ var rdoc_rel_prefix = "./";
13
+ </script>
14
+
15
+ <script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
16
+ <script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
17
+ <script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
18
+ <script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
19
+ <script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
20
+ <script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>
21
+
22
+
23
+ <body id="top" class="class">
24
+ <nav id="metadata">
25
+ <nav id="home-section" class="section">
26
+ <h3 class="section-header">
27
+ <a href="./index.html">Home</a>
28
+ <a href="./table_of_contents.html#classes">Classes</a>
29
+ <a href="./table_of_contents.html#methods">Methods</a>
30
+ </h3>
31
+ </nav>
32
+
33
+
34
+ <nav id="search-section" class="section project-section" class="initially-hidden">
35
+ <form action="#" method="get" accept-charset="utf-8">
36
+ <h3 class="section-header">
37
+ <input type="text" name="search" placeholder="Search" id="search-field"
38
+ title="Type to search, Up and Down to navigate, Enter to load">
39
+ </h3>
40
+ </form>
41
+
42
+ <ul id="search-results" class="initially-hidden"></ul>
43
+ </nav>
44
+
45
+
46
+ <div id="file-metadata">
47
+ <nav id="file-list-section" class="section">
48
+ <h3 class="section-header">Defined In</h3>
49
+ <ul>
50
+ <li>lib/shenanigans/hash/has_shape_pred.rb
51
+ </ul>
52
+ </nav>
53
+
54
+
55
+ </div>
56
+
57
+ <div id="class-metadata">
58
+
59
+ <nav id="parent-class-section" class="section">
60
+ <h3 class="section-header">Parent</h3>
61
+
62
+ <p class="link"><a href="Object.html">Object</a>
63
+
64
+ </nav>
65
+
66
+
67
+ <!-- Method Quickref -->
68
+ <nav id="method-list-section" class="section">
69
+ <h3 class="section-header">Methods</h3>
70
+
71
+ <ul class="link-list">
72
+
73
+ <li><a href="#method-i-has_shape-3F">#has_shape?</a>
74
+
75
+ </ul>
76
+ </nav>
77
+
78
+ </div>
79
+
80
+ <div id="project-metadata">
81
+ <nav id="fileindex-section" class="section project-section">
82
+ <h3 class="section-header">Pages</h3>
83
+
84
+ <ul>
85
+
86
+ <li class="file"><a href="./README_rdoc.html">README</a>
87
+
88
+ </ul>
89
+ </nav>
90
+
91
+ <nav id="classindex-section" class="section project-section">
92
+ <h3 class="section-header">Class and Module Index</h3>
93
+
94
+ <ul class="link-list">
95
+
96
+ <li><a href="./Array.html">Array</a>
97
+
98
+ <li><a href="./Hash.html">Hash</a>
99
+
100
+ <li><a href="./Kernel.html">Kernel</a>
101
+
102
+ <li><a href="./Object.html">Object</a>
103
+
104
+ </ul>
105
+ </nav>
106
+
107
+ </div>
108
+ </nav>
109
+
110
+ <div id="documentation">
111
+ <h1 class="class">class Hash</h1>
112
+
113
+ <div id="description" class="description">
114
+
115
+ </div><!-- description -->
116
+
117
+
118
+
119
+
120
+ <section id="5Buntitled-5D" class="documentation-section">
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+ <!-- Methods -->
130
+
131
+ <section id="public-instance-5Buntitled-5D-method-details" class="method-section section">
132
+ <h3 class="section-header">Public Instance Methods</h3>
133
+
134
+
135
+ <div id="method-i-has_shape-3F" class="method-detail ">
136
+
137
+ <div class="method-heading">
138
+ <span class="method-name">has_shape?</span><span
139
+ class="method-args">(shape)</span>
140
+ <span class="method-click-advice">click to toggle source</span>
141
+ </div>
142
+
143
+
144
+ <div class="method-description">
145
+
146
+ <p>Checks if a hash has a certain structure.</p>
147
+
148
+ <pre class="ruby"><span class="ruby-identifier">h</span> = { <span class="ruby-identifier">k1</span><span class="ruby-operator">:</span> <span class="ruby-value">1</span>, <span class="ruby-identifier">k2</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;1&quot;</span> }
149
+ <span class="ruby-identifier">h</span>.<span class="ruby-identifier">has_shape?</span>(<span class="ruby-identifier">k1</span><span class="ruby-operator">:</span> <span class="ruby-constant">Fixnum</span>, <span class="ruby-identifier">k2</span><span class="ruby-operator">:</span> <span class="ruby-constant">String</span>)
150
+ <span class="ruby-comment">#=&gt; true</span>
151
+ <span class="ruby-identifier">h</span>.<span class="ruby-identifier">has_shape?</span>(<span class="ruby-identifier">k1</span><span class="ruby-operator">:</span> <span class="ruby-constant">Class</span>, <span class="ruby-identifier">k2</span><span class="ruby-operator">:</span> <span class="ruby-constant">String</span>)
152
+ <span class="ruby-comment">#=&gt; false</span>
153
+ </pre>
154
+
155
+ <p>It also works with compound data structures.</p>
156
+
157
+ <pre class="ruby"><span class="ruby-identifier">h</span> = { <span class="ruby-identifier">k1</span><span class="ruby-operator">:</span> [], <span class="ruby-identifier">k2</span><span class="ruby-operator">:</span> { <span class="ruby-identifier">k3</span><span class="ruby-operator">:</span> <span class="ruby-constant">Struct</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;Foo&quot;</span>) } }
158
+ <span class="ruby-identifier">shape</span> = { <span class="ruby-identifier">k1</span><span class="ruby-operator">:</span> <span class="ruby-constant">Array</span>, <span class="ruby-identifier">k2</span><span class="ruby-operator">:</span> { <span class="ruby-identifier">k3</span><span class="ruby-operator">:</span> <span class="ruby-constant">Module</span> } }
159
+ <span class="ruby-identifier">h</span>.<span class="ruby-identifier">has_shape?</span>(<span class="ruby-identifier">shape</span>)
160
+ <span class="ruby-comment">#=&gt; true</span>
161
+ </pre>
162
+
163
+
164
+
165
+ <div class="method-source-code" id="has_shape-3F-source">
166
+ <pre><span class="ruby-comment"># File lib/shenanigans/hash/has_shape_pred.rb, line 13</span>
167
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">has_shape?</span>(<span class="ruby-identifier">shape</span>)
168
+ <span class="ruby-identifier">all?</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
169
+ <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">v</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">v</span>.<span class="ruby-identifier">has_shape?</span>(<span class="ruby-identifier">shape</span>[<span class="ruby-identifier">k</span>]) <span class="ruby-operator">:</span> <span class="ruby-identifier">shape</span>[<span class="ruby-identifier">k</span>] <span class="ruby-operator">===</span> <span class="ruby-identifier">v</span>
170
+ <span class="ruby-keyword">end</span>
171
+ <span class="ruby-keyword">end</span></pre>
172
+ </div><!-- has_shape-3F-source -->
173
+
174
+ </div>
175
+
176
+
177
+
178
+
179
+ </div><!-- has_shape-3F-method -->
180
+
181
+
182
+ </section><!-- public-instance-method-details -->
183
+
184
+ </section><!-- 5Buntitled-5D -->
185
+
186
+ </div><!-- documentation -->
187
+
188
+
189
+ <footer id="validator-badges">
190
+ <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
191
+ <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.12.
192
+ <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
193
+ </footer>
194
+
data/doc/Kernel.html CHANGED
@@ -95,6 +95,8 @@
95
95
 
96
96
  <li><a href="./Array.html">Array</a>
97
97
 
98
+ <li><a href="./Hash.html">Hash</a>
99
+
98
100
  <li><a href="./Kernel.html">Kernel</a>
99
101
 
100
102
  <li><a href="./Object.html">Object</a>
data/doc/Object.html CHANGED
@@ -100,6 +100,8 @@
100
100
 
101
101
  <li><a href="./Array.html">Array</a>
102
102
 
103
+ <li><a href="./Hash.html">Hash</a>
104
+
103
105
  <li><a href="./Kernel.html">Kernel</a>
104
106
 
105
107
  <li><a href="./Object.html">Object</a>
data/doc/README_rdoc.html CHANGED
@@ -61,6 +61,8 @@
61
61
 
62
62
  <li><a href="./Array.html">Array</a>
63
63
 
64
+ <li><a href="./Hash.html">Hash</a>
65
+
64
66
  <li><a href="./Kernel.html">Kernel</a>
65
67
 
66
68
  <li><a href="./Object.html">Object</a>
data/doc/created.rid CHANGED
@@ -1,8 +1,10 @@
1
- Fri, 25 May 2012 17:35:20 +0200
1
+ Tue, 19 Jun 2012 21:25:09 +0200
2
2
  README.rdoc Thu, 17 May 2012 11:45:08 +0200
3
3
  lib/shenanigans/array/random_subarray.rb Thu, 17 May 2012 00:03:23 +0200
4
- lib/shenanigans/array/zip_with.rb Thu, 17 May 2012 00:12:10 +0200
4
+ lib/shenanigans/array/zip_with.rb Tue, 19 Jun 2012 21:23:27 +0200
5
5
  lib/shenanigans/array.rb Wed, 16 May 2012 20:09:21 +0200
6
+ lib/shenanigans/hash/has_shape_pred.rb Tue, 19 Jun 2012 21:25:03 +0200
7
+ lib/shenanigans/hash.rb Tue, 19 Jun 2012 21:04:00 +0200
6
8
  lib/shenanigans/kernel/fn.rb Wed, 16 May 2012 23:57:36 +0200
7
9
  lib/shenanigans/kernel/prompt.rb Thu, 17 May 2012 00:10:42 +0200
8
10
  lib/shenanigans/kernel/with.rb Thu, 17 May 2012 00:00:08 +0200
@@ -10,4 +12,4 @@ lib/shenanigans/kernel.rb Wed, 16 May 2012 20:49:22 +0200
10
12
  lib/shenanigans/object/display.rb Fri, 25 May 2012 17:29:45 +0200
11
13
  lib/shenanigans/object/identity.rb Wed, 16 May 2012 23:51:07 +0200
12
14
  lib/shenanigans/object.rb Fri, 25 May 2012 15:31:08 +0200
13
- lib/shenanigans.rb Wed, 16 May 2012 21:04:23 +0200
15
+ lib/shenanigans.rb Tue, 19 Jun 2012 21:04:30 +0200
data/doc/index.html CHANGED
@@ -61,6 +61,8 @@
61
61
 
62
62
  <li><a href="./Array.html">Array</a>
63
63
 
64
+ <li><a href="./Hash.html">Hash</a>
65
+
64
66
  <li><a href="./Kernel.html">Kernel</a>
65
67
 
66
68
  <li><a href="./Object.html">Object</a>
@@ -1 +1 @@
1
- var search_data = {"index":{"searchIndex":["array","kernel","object","d()","display()","fn()","identity()","prompt()","random_subarray()","with()","zip_with()","readme"],"longSearchIndex":["array","kernel","object","object#d()","object#display()","kernel#fn()","object#identity()","kernel#prompt()","array#random_subarray()","kernel#with()","array#zip_with()",""],"info":[["Array","","Array.html","",""],["Kernel","","Kernel.html","",""],["Object","","Object.html","",""],["d","Object","Object.html#method-i-d","(new_line = true)",""],["display","Object","Object.html#method-i-display","(new_line = true)","<p>Outputs the object and also returns it. Will use <code>puts</code> if\n<code>new_line</code> is <code>true</code> and <code>print</code>\notherwise.\n\n<pre>&quot;foo&quot;.display ...</pre>\n"],["fn","Kernel","Kernel.html#method-i-fn","(*funs)","<p>Composes a list of functions. Functions can be specified as symbols or\nlambdas.\n\n<pre>[&quot;foo bar&quot;, &quot;baz qux&quot;].map ...</pre>\n"],["identity","Object","Object.html#method-i-identity","()","<p>An identity method that provides access to an object’s <code>self</code>.\n\n<pre>[1,2,3,4,5,1,2,2,3].group_by(&amp;:identity) ...</pre>\n"],["prompt","Kernel","Kernel.html#method-i-prompt","(text='', conversion=nil)","<p>Displays a prompt and returns chomped input. Modelled after the Python\nmethod <code>raw_input</code>, but also can …\n"],["random_subarray","Array","Array.html#method-i-random_subarray","(n=1)","<p>Generates random subarrays. Uses random numbers and bit-fiddling to assure\nperformant uniform distributions …\n"],["with","Kernel","Kernel.html#method-i-with","(o, &blk)","<p>A Pascal/ActionScript like <code>with</code> method. Yields its argument to\nthe provided block and then returns it. …\n"],["zip_with","Array","Array.html#method-i-zip_with","(other, op=nil)","<p>Zip <code>self</code> with <code>other</code>, combining the elements with\nthe provided block or symbol. The resulting array will …\n"],["README","","README_rdoc.html","","<p>Shenanigans\n<p><strong>shenanigan</strong> (plural shenanigans):\n<p>Trickery, games; skulduggery. To “call”, “claim” …\n"]]}}
1
+ var search_data = {"index":{"searchIndex":["array","hash","kernel","object","d()","display()","fn()","has_shape?()","identity()","prompt()","random_subarray()","with()","zip_with()","readme"],"longSearchIndex":["array","hash","kernel","object","object#d()","object#display()","kernel#fn()","hash#has_shape?()","object#identity()","kernel#prompt()","array#random_subarray()","kernel#with()","array#zip_with()",""],"info":[["Array","","Array.html","",""],["Hash","","Hash.html","",""],["Kernel","","Kernel.html","",""],["Object","","Object.html","",""],["d","Object","Object.html#method-i-d","(new_line = true)",""],["display","Object","Object.html#method-i-display","(new_line = true)","<p>Outputs the object and also returns it. Will use <code>puts</code> if\n<code>new_line</code> is <code>true</code> and <code>print</code>\notherwise.\n\n<pre>&quot;foo&quot;.display ...</pre>\n"],["fn","Kernel","Kernel.html#method-i-fn","(*funs)","<p>Composes a list of functions. Functions can be specified as symbols or\nlambdas.\n\n<pre>[&quot;foo bar&quot;, &quot;baz qux&quot;].map ...</pre>\n"],["has_shape?","Hash","Hash.html#method-i-has_shape-3F","(shape)","<p>Checks if a hash has a certain structure.\n\n<pre>h = { k1: 1, k2: &quot;1&quot; }\nh.has_shape?(k1: Fixnum, k2: String) ...</pre>\n"],["identity","Object","Object.html#method-i-identity","()","<p>An identity method that provides access to an object’s <code>self</code>.\n\n<pre>[1,2,3,4,5,1,2,2,3].group_by(&amp;:identity) ...</pre>\n"],["prompt","Kernel","Kernel.html#method-i-prompt","(text='', conversion=nil)","<p>Displays a prompt and returns chomped input. Modelled after the Python\nmethod <code>raw_input</code>, but also can …\n"],["random_subarray","Array","Array.html#method-i-random_subarray","(n=1)","<p>Generates random subarrays. Uses random numbers and bit-fiddling to assure\nperformant uniform distributions …\n"],["with","Kernel","Kernel.html#method-i-with","(o, &blk)","<p>A Pascal/ActionScript like <code>with</code> method. Yields its argument to\nthe provided block and then returns it. …\n"],["zip_with","Array","Array.html#method-i-zip_with","(other, op=nil)","<p>Zip <code>self</code> with <code>other</code>, combining the elements with\nthe provided block or symbol. The resulting array will …\n"],["README","","README_rdoc.html","","<p>Shenanigans\n<p><strong>shenanigan</strong> (plural shenanigans):\n<p>Trickery, games; skulduggery. To “call”, “claim” …\n"]]}}
@@ -46,6 +46,9 @@
46
46
  <ul>
47
47
  <li class="class">
48
48
  <a href="Array.html">Array</a>
49
+ </li>
50
+ <li class="class">
51
+ <a href="Hash.html">Hash</a>
49
52
  </li>
50
53
  <li class="module">
51
54
  <a href="Kernel.html">Kernel</a>
@@ -65,6 +68,8 @@
65
68
 
66
69
  <li class="method"><a href="Kernel.html#method-i-fn">#fn &mdash; Kernel</a>
67
70
 
71
+ <li class="method"><a href="Hash.html#method-i-has_shape-3F">#has_shape? &mdash; Hash</a>
72
+
68
73
  <li class="method"><a href="Object.html#method-i-identity">#identity &mdash; Object</a>
69
74
 
70
75
  <li class="method"><a href="Kernel.html#method-i-prompt">#prompt &mdash; Kernel</a>
@@ -11,7 +11,7 @@ class Array
11
11
  #
12
12
  # For more complex combinations, a block can be provided:
13
13
  #
14
- # [1,2,3].zw([6,5,4]) { |a,b| 3*a+2*b }
14
+ # [1,2,3].zip_with([6,5,4]) { |a,b| 3*a+2*b }
15
15
  # #=> [15, 16, 17]
16
16
  def zip_with(other, op=nil)
17
17
  return [] if self.empty? || other.empty?
@@ -0,0 +1,18 @@
1
+ class Hash
2
+ # Checks if a hash has a certain structure.
3
+ # h = { k1: 1, k2: "1" }
4
+ # h.has_shape?(k1: Fixnum, k2: String)
5
+ # #=> true
6
+ # h.has_shape?(k1: Class, k2: String)
7
+ # #=> false
8
+ # It also works with compound data structures.
9
+ # h = { k1: [], k2: { k3: Struct.new("Foo") } }
10
+ # shape = { k1: Array, k2: { k3: Module } }
11
+ # h.has_shape?(shape)
12
+ # #=> true
13
+ def has_shape?(shape)
14
+ all? do |k, v|
15
+ Hash === v ? v.has_shape?(shape[k]) : shape[k] === v
16
+ end
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ require_relative 'hash/has_shape_pred'
data/lib/shenanigans.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'shenanigans/array'
2
+ require 'shenanigans/hash'
2
3
  require 'shenanigans/kernel'
3
4
  require 'shenanigans/object'
@@ -0,0 +1,40 @@
1
+ require 'minitest/autorun'
2
+ require 'shenanigans/hash/has_shape_pred'
3
+
4
+ class HasShape < MiniTest::Unit::TestCase
5
+ def setup
6
+ @hash = {
7
+ k1: 1.0,
8
+ k2: 1,
9
+ k3: {
10
+ k4: "1",
11
+ k5: {
12
+ k6: Array
13
+ }
14
+ }
15
+ }
16
+ end
17
+
18
+ def test_has_shape
19
+ shape_pass = {
20
+ k1: Float,
21
+ k2: Integer,
22
+ k3: {
23
+ k4: String,
24
+ k5: {
25
+ k6: Class
26
+ }
27
+ }
28
+ }
29
+
30
+ assert @hash.has_shape?(shape_pass)
31
+ end
32
+
33
+ def test_has_shape_fail
34
+ shape_fail = {
35
+ k1: Float
36
+ }
37
+
38
+ assert !@hash.has_shape?(shape_fail)
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shenanigans
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-25 00:00:00.000000000 Z
12
+ date: 2012-06-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: citizen428@gmail.com
@@ -22,6 +22,7 @@ files:
22
22
  - README.rdoc
23
23
  - doc/Array.html
24
24
  - doc/created.rid
25
+ - doc/Hash.html
25
26
  - doc/images/add.png
26
27
  - doc/images/brick.png
27
28
  - doc/images/brick_link.png
@@ -61,6 +62,8 @@ files:
61
62
  - lib/shenanigans/array/random_subarray.rb
62
63
  - lib/shenanigans/array/zip_with.rb
63
64
  - lib/shenanigans/array.rb
65
+ - lib/shenanigans/hash/has_shape_pred.rb
66
+ - lib/shenanigans/hash.rb
64
67
  - lib/shenanigans/kernel/fn.rb
65
68
  - lib/shenanigans/kernel/prompt.rb
66
69
  - lib/shenanigans/kernel/with.rb
@@ -73,6 +76,7 @@ files:
73
76
  - lib/shenanigans.rb
74
77
  - test/array/test_random_subarray.rb
75
78
  - test/array/test_zip_with.rb
79
+ - test/hash/test_has_shape_pred.rb
76
80
  - test/kernel/test_fn.rb
77
81
  - test/kernel/test_prompt.rb
78
82
  - test/kernel/test_with.rb