From 8a366d5275e1a2b66c17b4d9f87fc538ea87b378 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 18 Mar 2015 20:09:04 -0500 Subject: [PATCH] Add Create An Array of Stringed Numbers as a ruby til. --- README.md | 1 + ruby/create-an-array-of-stringed-numbers.md | 28 +++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 ruby/create-an-array-of-stringed-numbers.md diff --git a/README.md b/README.md index bc5eae9..d674fe7 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ smart people at [Hashrocket](http://hashrocket.com/). ### ruby +- [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md) - [Limit Split](ruby/limit-split.md) - [Parallel Bundle Install](ruby/parallel-bundle-install.md) - [Summing Collections](ruby/summing-collections.md) diff --git a/ruby/create-an-array-of-stringed-numbers.md b/ruby/create-an-array-of-stringed-numbers.md new file mode 100644 index 0000000..cabb6c3 --- /dev/null +++ b/ruby/create-an-array-of-stringed-numbers.md @@ -0,0 +1,28 @@ +# Create an Array of Stringed Numbers + +To create an array of numbers between `1` and `5`, I can do something like +the following: + +```ruby +(1..5).to_a +> [1,2,3,4,5] +``` + +However, what if I want an array of `"1"` to `"5"`? That is, what if I want +an array of consecutive numbers as strings? + +I can just reuse the above and map them to strings like so: + +```ruby +(1..5).to_a.map(&:to_s) +> ['1','2','3','4','5'] +``` + +Though, that seems more verbose than necessary. Ruby actually allows you to +do ranges of characters. Which means that I can modify my original approach +to look something like this: + +```ruby +('1'..'5').to_a +> ['1','2','3','4','5'] +```