DEV Community

Discussion on: Picking up Ruby, as a Python Dev & a mini fish app

Collapse
 
topofocus profile image
Hartmut B.

You can easily rewrite

  def show_timeline
    puts "Fishify: #{@title} has tracked #{@levels.count} Levels"

    @levels.sort_by( &:time ) 
                 .reverse
                 .each { |t|  puts "#{t.ph} #{t.ammonia} #{t.nitrites} #{t.nitrates} #{t.temp{t.time}"    }
  end
end
Enter fullscreen mode Exit fullscreen mode

Next: the output in the iteration is not recommended.
map and join are your friends.

  def show_timeline
    puts "Fishify: #{@title} has tracked #{@levels.count} Levels"
    puts @levels.sort_by( &:time ) 
                         .reverse
                         .map{ |t| [t.ph, t.ammonia, t.nitrites, t.nitrates, t.time].join(" ")}
                         .join "\n"
 end
Enter fullscreen mode Exit fullscreen mode

Then the code is quite different from any python program, I guess. To me its more readable. Don't now, how this feels from your perspective,

Collapse
 
vickilanger profile image
Vicki Langer

Oh yeah! I was already told I should probably use .map most times that I see .each. Forgot about that.

adds to notes "no output in an iteration"

I can see how this works better. Thanks!