Created
January 4, 2013 03:06
-
-
Save kmandreza/4449595 to your computer and use it in GitHub Desktop.
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. For example, ruby [1,2,3].pad(5) should return [1,2,3,nil,nil]
And ruby [1…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Array | |
| def pad!(min_size, value = nil) | |
| x = min_size - self.count #take the argument and subtract the count from it. | |
| x.times do | |
| self << value | |
| end# I want to add the value x number of times to the array | |
| self | |
| end | |
| def pad(min_size, value = nil) | |
| self.clone.pad!(min_size, value) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment