Created
March 7, 2013 08:23
-
-
Save jkaihsu/5106430 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 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 | |
x.times {self << value} | |
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