Skip to content

Instantly share code, notes, and snippets.

@jkaihsu
Created March 7, 2013 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkaihsu/5106430 to your computer and use it in GitHub Desktop.
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…
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