coding style - Quick Advice: How should this be written in Ruby? -


I am a Java / C ++ programmer and Ruby is my first scripting language. I sometimes find that I Not as productive as I can in some areas, such as this for example:

Purpose: To parse only a few rows from the file. The method I am going with is that there is a very large line with size greater than 15, the rest are definitely small. I want to ignore all the big (and inclusive) lines.

  def does_something (str) puts str end str = 'me too ignore me! The big line is its line! Target1 target2 target3 'flag1 = zero str.each_line do | Line | Do_something (line) if flag1 flag1 = 1 if line.size & gt; 15 end  

I wrote this, but I think it can be written much better, i.e., should be a better way than setting a flag. Recommendations of how Ruby's beautiful lines also welcome.

Note / Explanation: I need to print all the lines after the first appearance of the big line.

  str.lines.drop_while {| L L.length & lt; 15} .drop (1) .each {| L I like it, because if you read it from left to right, then it reads like your original description at all: 

Split the string into it lines and drop rows less than 15 characters then another Drop the line (i.e. the first one with more than 14 characters). Then do something with each remaining line.

You do not have to be able to understand the essential Ruby, or all programming that it is correct.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -