mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 00:58:02 +00:00
Add Create An Array of Stringed Numbers as a ruby til.
This commit is contained in:
28
ruby/create-an-array-of-stringed-numbers.md
Normal file
28
ruby/create-an-array-of-stringed-numbers.md
Normal file
@@ -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']
|
||||
```
|
||||
Reference in New Issue
Block a user