Skip to content

Instantly share code, notes, and snippets.

@UzmaKR
Created April 12, 2013 00:11
Show Gist options
  • Save UzmaKR/5368243 to your computer and use it in GitHub Desktop.
Save UzmaKR/5368243 to your computer and use it in GitHub Desktop.
Exercise: Create a method to pad an array
class Array
def pad!(min_size, value = nil)
if min_size <= self.length
return self
else
num_add=min_size - self.length
num_add.times do
self.push(value)
end
return self
end
end
def pad(min_size, value = nil)
if min_size <= self.length
return self
else
diff_arr=self.dup
num_add=min_size - diff_arr.length
num_add.times do
diff_arr.push(value)
end
return diff_arr
end
end
end
Error!
Array#pad operates non-destructively
expected: value != 15541420 got: 15541420 (compared using ==)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment