DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Fill The Missing Blanks In An Array

02.12.2010
| 3314 views |
  • submit to reddit
        If we have an array which has only 2 items yet we expected 4 items we can use Array#fill to complete it e.g.
r = %w(b c)       # => ["b", "c"]
r.fill('a', 2..3) # => ["b", "c", "a", "a"]

Resources:
 - <a href="http://ruby-doc.org/core/classes/Array.html#M002202">Class: Array</a> [ruby-doc.org]