Skip to content

Instantly share code, notes, and snippets.

@isomorpheric
Last active February 14, 2016 23:15
Show Gist options
  • Save isomorpheric/9efc323d24d56252f50f to your computer and use it in GitHub Desktop.
Save isomorpheric/9efc323d24d56252f50f to your computer and use it in GitHub Desktop.
Image blur 2. Can't get it to work.
# Image Blur 1
class Image
def initialize(array)
@array = array
end
def output_image
@array.each do |row|
puts row.join(" ")
end
nil
end
def blur
@copy = @array
@array.each.with_index do |row, row_index|
row.each.with_index do |pixel, col_index|
if pixel == 1
@copy.transform(row_index, col_index)
end
end
end
return @copy
end
def transform(row_index, col_index)
@array[row_index - 1][col_index] = 1
@array[row_index + 1][col_index] = 1
@array[row_index][col_index - 1] = 1
@array[row_index][col_index + 1] = 1
end
end
# ------------------------------------------------------------------tests
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
image.blur
image.output_image
Copy link

ghost commented Feb 14, 2016

It looks like you are on the right track. Try removing the @copy and just call the transform method. Lines 30 and 32 were giving me nil errors for the int value 1 when I tested them on Codecademy Labs. Other values such as 2, 3, 4, etc seemed to test out fine. I'm guessing that you will probably have to check the row and column boundaries before setting the values. Hope that helps.

@woodbrettm
Copy link

Couple of notes. I learned this the hard way, but by setting @copy = @array, you're linking the two together such that changes on one are made to the other. So when you do @array[row_index][col_index + 1] = 1, the program hangs. In order to create the copy such that it is separate from the initial array, you can do the following: @array.each { |row| @copy << row.dup }. Also, for testing purposes, I'd ditch the transform method and include it in the blur method, since they're both a part of achieving the same task, however that's just my opinion and up to you :). Lastly, don't forget to check for edge cases. If you're at col_index of [0] and you do [col_index - 1], that equals to @array[row_index][-1]. [-1] is a way to access the last index of the array, so even though you're at index 0, changing values at [-1] will in this case, change the value of the 3rd index of your column. So if you had [1, 0, 0, 0] at the top row and set if pixel == 1, @array[row_index][col_index - 1] = 1, the top row would then look like this: [1, 0, 0, 1]. This issue also applies to row_index, and also check for issues when you're at the last index in each column or row, and you want to access [+1] of that index (which won't exist). Hope that helps m8 good luck coding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment