-
-
Save UzmaKR/5368243 to your computer and use it in GitHub Desktop.
Exercise: Create a method to pad an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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