DEV Community

Cover image for Ruby As A Second Language
Arit Developer
Arit Developer

Posted on • Updated on • Originally published at arit.dev

Ruby As A Second Language

This week’s coding challenge in my bootcamp had my brain all knotted up. I was already fatigued from solving (most of) 32 CodeWars katas, but I was scheduled to discuss said challenge — titled “Image Blur 2” — with my mentor Jeremy. I decided to see if I could make any sense of the challenge, which went as follows:

Using Ruby, transform a grid of ones and zeroes such that any zeroes above, beneath and to the sides of a “1” are turned to “1”s (as depicted in the image below the pink "1s" used to be zeroes)

alt text

We are currently learning object-oriented programming, so I knew the resulting algorithm would consist of Class and Method definitions. But not even a snippet of a solution would come to me — not that evening, anyhow. So I decided to describe my would-be code in plain English:

  1. Locate the cells with a value of “1”.
  2. Check whether there are rows above, beneath or to the sides of said cell.
  3. Check whether the cells adjacent to said cell in these rows have a value of 1.
  4. If yes, leave unchanged; if not, change the value of adjacent cells to 1. Location of cell-in-question: array[row][column] Cell above: array[row-1][column] Cell below: array[row+1][column] Cell on Left: array[row][column-1] Cell on Right: array[row][column+1]

The only code-y part of my write-up is the location of an element within the grid (“array[row][column]”). I emailed my write-up to Jeremy and called it a night. During our session the next day, Jeremy explained that the challenge consisted of 2 main objectives, and that my “vague” conceptualization had all-but-fulfilled the harder objective (whaa???). We spent the rest of the evening translating my write-up to actual code and creating test-cases to ensure my algorithm’s robustness.

This experience has markedly increased my confidence in approaching algorithms; before now, I believed that if I wasn’t “thinking in Ruby” from the get, then my work wasn’t really legit. After all, it is said that native speakers of a language THINK (not just speak) in that language, right? Well, I’m certainly not expert in Ruby (yet!), but I realize that it’s okay — even effective! — to translate from a language that I’m already comfortable with.

Top comments (4)

Collapse
 
rpalo profile image
Ryan Palo

This is super cool! I love Ruby, so thanks for sharing :)

BTW, if you run into any grid/matrix problems in the future that you want to use Ruby for, check out the matrix library that's part of Ruby's standard library.

require 'matrix'

grid = Matrix[[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
grid[1, 1]
# => 1
grid[2, 1]
# => 0

:)

Collapse
 
alephnaught2tog profile image
Max Cerrina • Edited

I'm curious, how would you explain this to someone if you were to walk them through step-by-step? I hear so often that a more functional approach can be harder to understand, and I wonder how you personally would approach explaining that code you posted to a beginner so that it was accessible to them, since I know you're very well-versed in functional programming et al and want to share functional programming concepts with others!

I know one thing I've found useful in such contexts when working with students is to use meaningful variable names even though they're longer.

Collapse
 
aritdeveloper profile image
Arit Developer

WOW! Hey, would you be open to pair-programming with me to discuss this solution? :D

 
alephnaught2tog profile image
Max Cerrina

That is awesome! I would love to read more breakdowns of problems like this.