From 27d062cc506cad746abb2e1c3720a7bf778c53f0 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 31 May 2016 19:03:27 -0500 Subject: [PATCH] Add Create A Hash From An Array Of Arrays as a ruby til --- README.md | 3 ++- ruby/create-a-hash-from-an-array-of-arrays.md | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 ruby/create-a-hash-from-an-array-of-arrays.md diff --git a/README.md b/README.md index 7a3ba70..c87833d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_426 TILs and counting..._ +_427 TILs and counting..._ --- @@ -298,6 +298,7 @@ _426 TILs and counting..._ - [Comparing Arrays In RSpec](ruby/comparing-arrays-in-rspec.md) - [Construct A Constant From A String](ruby/construct-a-constant-from-a-string.md) - [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md) +- [Create A Hash From An Array Of Arrays](ruby/create-a-hash-from-an-array-of-arrays) - [Create Thumbnail Image For A PDF](ruby/create-thumbnail-image-for-a-pdf.md) - [Defaulting To Frozen String Literals](ruby/defaulting-to-frozen-string-literals.md) - [Destructuring Arrays In Blocks](ruby/destructuring-arrays-in-blocks.md) diff --git a/ruby/create-a-hash-from-an-array-of-arrays.md b/ruby/create-a-hash-from-an-array-of-arrays.md new file mode 100644 index 0000000..ea00a7d --- /dev/null +++ b/ruby/create-a-hash-from-an-array-of-arrays.md @@ -0,0 +1,16 @@ +# Create A Hash From An Array Of Arrays + +The `::[]` method on the `Hash` class allows you to succinctly create a hash +from an array of arrays -- or rather an array of tuples which are key value +pairs. + +```ruby +> Hash[ [["a",2],["b",4]] ] +{"a"=>2, "b"=>4} +> Hash[ [[1,2],[3,4]] ] +{1=>2, 3=>4} +``` + +See the [`Hash::[]` +docs](http://ruby-doc.org/core-2.3.0/Hash.html#method-c-5B-5D) for more +details.