On re-reading old code

I came across this beauty:

def same_modality? list
  check = list[0][0]
  list.each do |i|
    return false if check != i[0]
  end
  return true
end

This was “necessary” because I got an array of one-element arrays back, and I wanted to check whether or not that one element was the same across the array.

Three seconds of thinking made me realize that just maybe, I could do this:

list.uniq.size == 1

I -like- Ruby.

Leave a Reply

You must be logged in to post a comment.