Skip to content

Instantly share code, notes, and snippets.

@kayline
Created March 20, 2013 02:46
Show Gist options
  • Save kayline/5201913 to your computer and use it in GitHub Desktop.
Save kayline/5201913 to your computer and use it in GitHub Desktop.
DBC Prework - Pad an array. In the second method, removing "clone" from line 17 causes the code to fail the 4th spec test, for padding the array with "apples". Why? It seems like all that should do is cause it to fail the 3rd spec test, for a non-destructive method.
class Array
def pad!(min_size, value = nil)
array = self
diff = min_size - self.length
if diff <= 0
return self
else
diff.times do
array.push(value)
end
return array
end
end
def pad(min_size, value = nil)
array = self.clone
array.pad!(min_size, value)
return array
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment