Skip to content

Instantly share code, notes, and snippets.

@Stephenitis
Last active December 14, 2015 07:48
Show Gist options
  • Save Stephenitis/5052937 to your computer and use it in GitHub Desktop.
Save Stephenitis/5052937 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]
class Array
def pad!(min_size, value = nil)
if self.length >= min_size
return self
else
add_amount = (min_size - self.length)
add_amount.times do self.push(value)
return self
end
end
end
def pad(min_size, value = nil)
newthing= self.clone
if self.length >= min_size
return newthing
else
add_amount = (min_size - self.length)
puts add_amount
add_amount.times{newthing.push(value)}
#end # add_amount.times
return newthing
end # if length
end # def pad
end
#thanks to Ben for the help at Arlington Ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment