shenanigans 1.0.5 → 1.0.6

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.
data/doc/Fixnum.html CHANGED
@@ -145,7 +145,7 @@
145
145
 
146
146
  <div class="method-description">
147
147
 
148
- <p>Returns the lenth of the number’s string representation</p>
148
+ <p>Returns the length of the number’s string representation.</p>
149
149
 
150
150
  <pre class="ruby"><span class="ruby-value">0</span>.<span class="ruby-identifier">string_length</span>
151
151
  <span class="ruby-comment">#=&gt; 1</span>
data/doc/Hash.html CHANGED
@@ -47,6 +47,7 @@
47
47
  <nav id="file-list-section" class="section">
48
48
  <h3 class="section-header">Defined In</h3>
49
49
  <ul>
50
+ <li>lib/shenanigans/hash/extract.rb
50
51
  <li>lib/shenanigans/hash/has_shape_pred.rb
51
52
  <li>lib/shenanigans/hash/to_ostruct.rb
52
53
  </ul>
@@ -71,6 +72,8 @@
71
72
 
72
73
  <ul class="link-list">
73
74
 
75
+ <li><a href="#method-i-extract">#extract</a>
76
+
74
77
  <li><a href="#method-i-has_shape-3F">#has_shape?</a>
75
78
 
76
79
  <li><a href="#method-i-to_ostruct">#to_ostruct</a>
@@ -137,6 +140,44 @@
137
140
  <h3 class="section-header">Public Instance Methods</h3>
138
141
 
139
142
 
143
+ <div id="method-i-extract" class="method-detail ">
144
+
145
+ <div class="method-heading">
146
+ <span class="method-name">extract</span><span
147
+ class="method-args">(*ks)</span>
148
+ <span class="method-click-advice">click to toggle source</span>
149
+ </div>
150
+
151
+
152
+ <div class="method-description">
153
+
154
+ <p>Returns a new hash only with the specified keys (if present).</p>
155
+
156
+ <pre class="ruby"><span class="ruby-identifier">hash</span> = { <span class="ruby-identifier">a</span><span class="ruby-operator">:</span> <span class="ruby-value">1</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">:</span> <span class="ruby-value">2</span>, <span class="ruby-identifier">c</span><span class="ruby-operator">:</span> <span class="ruby-value">3</span> }
157
+ <span class="ruby-identifier">hash</span>.<span class="ruby-identifier">extract</span>(:<span class="ruby-identifier">b</span>, :<span class="ruby-identifier">a</span>)
158
+ <span class="ruby-comment">#=&gt; { a: 1, b: 2 }</span>
159
+ <span class="ruby-identifier">hash</span>.<span class="ruby-identifier">extract</span>(:<span class="ruby-identifier">a</span>, :<span class="ruby-identifier">d</span>)
160
+ <span class="ruby-comment">#=&gt; { a: 1}</span>
161
+ </pre>
162
+
163
+
164
+
165
+ <div class="method-source-code" id="extract-source">
166
+ <pre><span class="ruby-comment"># File lib/shenanigans/hash/extract.rb, line 8</span>
167
+ <span class="ruby-keyword">def</span> <span class="ruby-identifier">extract</span>(*<span class="ruby-identifier">ks</span>)
168
+ <span class="ruby-identifier">existing</span> = <span class="ruby-identifier">keys</span> &amp; <span class="ruby-identifier">ks</span>
169
+ <span class="ruby-constant">Hash</span>[<span class="ruby-identifier">existing</span>.<span class="ruby-identifier">zip</span>(<span class="ruby-identifier">values_at</span>(*<span class="ruby-identifier">existing</span>))]
170
+ <span class="ruby-keyword">end</span></pre>
171
+ </div><!-- extract-source -->
172
+
173
+ </div>
174
+
175
+
176
+
177
+
178
+ </div><!-- extract-method -->
179
+
180
+
140
181
  <div id="method-i-has_shape-3F" class="method-detail ">
141
182
 
142
183
  <div class="method-heading">
data/doc/created.rid CHANGED
@@ -1,14 +1,15 @@
1
- Sun, 15 Jul 2012 00:00:08 +0200
1
+ Thu, 09 Aug 2012 18:48:00 +0200
2
2
  README.rdoc Thu, 17 May 2012 11:45:08 +0200
3
3
  lib/shenanigans/array/caret.rb Thu, 28 Jun 2012 09:15:50 +0200
4
4
  lib/shenanigans/array/random_subarray.rb Thu, 17 May 2012 00:03:23 +0200
5
5
  lib/shenanigans/array/zip_with.rb Tue, 19 Jun 2012 21:23:27 +0200
6
6
  lib/shenanigans/array.rb Thu, 28 Jun 2012 09:00:29 +0200
7
- lib/shenanigans/fixnum/string_length.rb Sun, 15 Jul 2012 00:00:03 +0200
7
+ lib/shenanigans/fixnum/string_length.rb Sun, 15 Jul 2012 00:00:21 +0200
8
8
  lib/shenanigans/fixnum.rb Sat, 14 Jul 2012 23:49:59 +0200
9
+ lib/shenanigans/hash/extract.rb Thu, 09 Aug 2012 18:47:48 +0200
9
10
  lib/shenanigans/hash/has_shape_pred.rb Tue, 19 Jun 2012 21:25:03 +0200
10
11
  lib/shenanigans/hash/to_ostruct.rb Tue, 26 Jun 2012 21:24:42 +0200
11
- lib/shenanigans/hash.rb Thu, 21 Jun 2012 19:13:44 +0200
12
+ lib/shenanigans/hash.rb Thu, 09 Aug 2012 18:32:30 +0200
12
13
  lib/shenanigans/kernel/fn.rb Wed, 16 May 2012 23:57:36 +0200
13
14
  lib/shenanigans/kernel/prompt.rb Thu, 17 May 2012 00:10:42 +0200
14
15
  lib/shenanigans/kernel/with.rb Thu, 17 May 2012 00:00:08 +0200
@@ -1 +1 @@
1
- var search_data = {"index":{"searchIndex":["array","fixnum","hash","kernel","object","^()","d()","display()","fn()","has_shape?()","it()","prompt()","random_subarray()","string_length()","to_ostruct()","with()","zip_with()","readme"],"longSearchIndex":["array","fixnum","hash","kernel","object","array#^()","object#d()","object#display()","kernel#fn()","hash#has_shape?()","object#it()","kernel#prompt()","array#random_subarray()","fixnum#string_length()","hash#to_ostruct()","kernel#with()","array#zip_with()",""],"info":[["Array","","Array.html","",""],["Fixnum","","Fixnum.html","",""],["Hash","","Hash.html","",""],["Kernel","","Kernel.html","",""],["Object","","Object.html","",""],["^","Array","Array.html#method-i-5E","(other)","<p>Returns an array containing elements exclusive between two arrays.\n\n<pre class=\"ruby\">[<span class=\"ruby-value\">1</span>, <span class=\"ruby-value\">2</span>, <span class=\"ruby-value\">3</span>] <span class=\"ruby-operator\">^</span> [<span class=\"ruby-value\">1</span>, <span class=\"ruby-value\">2</span>, <span class=\"ruby-value\">4</span>]\n<span class=\"ruby-comment\">#=&gt; [3, 4]</span>\n</pre>\n"],["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"],["it","Object","Object.html#method-i-it","()","<p>An identity method that provides access to an object’s <code>self</code>.\n\n<pre class=\"ruby\">[<span class=\"ruby-value\">1</span>,<span class=\"ruby-value\">2</span>,<span class=\"ruby-value\">3</span>,<span class=\"ruby-value\">4</span>,<span class=\"ruby-value\">5</span>,<span class=\"ruby-value\">1</span>,<span class=\"ruby-value\">2</span>,<span class=\"ruby-value\">2</span>,<span class=\"ruby-value\">3</span>].<span class=\"ruby-identifier\">group_by</span>(&amp;:<span class=\"ruby-identifier\">it</span>)\n<span class=\"ruby-comment\">#=&gt; {1=&gt;[1, ...</span>\n</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"],["string_length","Fixnum","Fixnum.html#method-i-string_length","()","<p>Returns the lenth of the number’s string representation\n\n<pre class=\"ruby\"><span class=\"ruby-value\">0</span>.<span class=\"ruby-identifier\">string_length</span>\n<span class=\"ruby-comment\">#=&gt; 1</span>\n<span class=\"ruby-value\">123</span>.<span class=\"ruby-identifier\">string_length</span>\n<span class=\"ruby-comment\">#=&gt; 3 ...</span>\n</pre>\n"],["to_ostruct","Hash","Hash.html#method-i-to_ostruct","()","<p>Recursively converts a <code>Hash</code> and all nested <code>Hash</code>es\nto <code>OpenStruct</code>s. Especially useful for parsing YAML …\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","fixnum","hash","kernel","object","^()","d()","display()","extract()","fn()","has_shape?()","it()","prompt()","random_subarray()","string_length()","to_ostruct()","with()","zip_with()","readme"],"longSearchIndex":["array","fixnum","hash","kernel","object","array#^()","object#d()","object#display()","hash#extract()","kernel#fn()","hash#has_shape?()","object#it()","kernel#prompt()","array#random_subarray()","fixnum#string_length()","hash#to_ostruct()","kernel#with()","array#zip_with()",""],"info":[["Array","","Array.html","",""],["Fixnum","","Fixnum.html","",""],["Hash","","Hash.html","",""],["Kernel","","Kernel.html","",""],["Object","","Object.html","",""],["^","Array","Array.html#method-i-5E","(other)","<p>Returns an array containing elements exclusive between two arrays.\n\n<pre class=\"ruby\">[<span class=\"ruby-value\">1</span>, <span class=\"ruby-value\">2</span>, <span class=\"ruby-value\">3</span>] <span class=\"ruby-operator\">^</span> [<span class=\"ruby-value\">1</span>, <span class=\"ruby-value\">2</span>, <span class=\"ruby-value\">4</span>]\n<span class=\"ruby-comment\">#=&gt; [3, 4]</span>\n</pre>\n"],["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"],["extract","Hash","Hash.html#method-i-extract","(*ks)","<p>Returns a new hash only with the specified keys (if present).\n\n<pre>hash = { a: 1, b: 2, c: 3 }\nhash.extract(:b, ...</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"],["it","Object","Object.html#method-i-it","()","<p>An identity method that provides access to an object’s <code>self</code>.\n\n<pre class=\"ruby\">[<span class=\"ruby-value\">1</span>,<span class=\"ruby-value\">2</span>,<span class=\"ruby-value\">3</span>,<span class=\"ruby-value\">4</span>,<span class=\"ruby-value\">5</span>,<span class=\"ruby-value\">1</span>,<span class=\"ruby-value\">2</span>,<span class=\"ruby-value\">2</span>,<span class=\"ruby-value\">3</span>].<span class=\"ruby-identifier\">group_by</span>(&amp;:<span class=\"ruby-identifier\">it</span>)\n<span class=\"ruby-comment\">#=&gt; {1=&gt;[1, ...</span>\n</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"],["string_length","Fixnum","Fixnum.html#method-i-string_length","()","<p>Returns the length of the number’s string representation.\n\n<pre class=\"ruby\"><span class=\"ruby-value\">0</span>.<span class=\"ruby-identifier\">string_length</span>\n<span class=\"ruby-comment\">#=&gt; 1</span>\n<span class=\"ruby-value\">123</span>.<span class=\"ruby-identifier\">string_length</span>\n<span class=\"ruby-comment\">#=&gt; ...</span>\n</pre>\n"],["to_ostruct","Hash","Hash.html#method-i-to_ostruct","()","<p>Recursively converts a <code>Hash</code> and all nested <code>Hash</code>es\nto <code>OpenStruct</code>s. Especially useful for parsing YAML …\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"]]}}
@@ -71,6 +71,8 @@
71
71
 
72
72
  <li class="method"><a href="Object.html#method-i-display">#display &mdash; Object</a>
73
73
 
74
+ <li class="method"><a href="Hash.html#method-i-extract">#extract &mdash; Hash</a>
75
+
74
76
  <li class="method"><a href="Kernel.html#method-i-fn">#fn &mdash; Kernel</a>
75
77
 
76
78
  <li class="method"><a href="Hash.html#method-i-has_shape-3F">#has_shape? &mdash; Hash</a>
@@ -1,2 +1,3 @@
1
+ require_relative 'hash/extract'
1
2
  require_relative 'hash/has_shape_pred'
2
3
  require_relative 'hash/to_ostruct'
@@ -0,0 +1,12 @@
1
+ class Hash
2
+ # Returns a new hash only with the specified keys (if present).
3
+ # hash = { a: 1, b: 2, c: 3 }
4
+ # hash.extract(:b, :a)
5
+ # #=> { a: 1, b: 2 }
6
+ # hash.extract(:a, :d)
7
+ # #=> { a: 1}
8
+ def extract(*ks)
9
+ existing = keys & ks
10
+ Hash[existing.zip(values_at(*existing))]
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'minitest/autorun'
2
+ require 'shenanigans/hash/extract'
3
+
4
+ class Extract < MiniTest::Unit::TestCase
5
+ def test_extract
6
+ hash = { a: 1, b: 2, c: 3 }
7
+ assert_equal hash.extract(:b, :a), { a: 1, b: 2 }
8
+ assert_equal hash.extract(:a, :d), { a: 1}
9
+ assert_equal Hash.new.extract(:a, :c), Hash.new
10
+ end
11
+ 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.5
4
+ version: 1.0.6
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-07-14 00:00:00.000000000 Z
12
+ date: 2012-08-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: citizen428@gmail.com
@@ -66,6 +66,7 @@ files:
66
66
  - lib/shenanigans/array.rb
67
67
  - lib/shenanigans/fixnum/string_length.rb
68
68
  - lib/shenanigans/fixnum.rb
69
+ - lib/shenanigans/hash/extract.rb
69
70
  - lib/shenanigans/hash/has_shape_pred.rb
70
71
  - lib/shenanigans/hash/to_ostruct.rb
71
72
  - lib/shenanigans/hash.rb
@@ -82,6 +83,7 @@ files:
82
83
  - test/array/test_random_subarray.rb
83
84
  - test/array/test_zip_with.rb
84
85
  - test/fixnum/test_string_length.rb
86
+ - test/hash/test_extract.rb
85
87
  - test/hash/test_has_shape_pred.rb
86
88
  - test/hash/test_to_ostruct.rb
87
89
  - test/kernel/test_fn.rb