Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created January 4, 2013 03:06
Show Gist options
  • Select an option

  • Save kmandreza/4449595 to your computer and use it in GitHub Desktop.

Select an option

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…
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