How to write ruby function to check for case insensitive word "error" in a file and return a property -


the function written me given below. work properly? else please correct me.

def log_file( file )       parsed_data = {}       read_lines( file ) {|line|           if line.match(/error/i)               parsed_data[:property] = "error"           end       }       return parsed_data   end   

the function may work properly, it's impossible tell you're using methods defined externally such read_lines.

also, it's not idiomatic ruby (especially explicit return , multi-line {} block).

here's possible alternative

def def log_file(file)   file.open(file).each_line |line|     return({ property: "error" }) if line =~ /error/i   end   {} end 

also, doesn't make lot of sense me return hash, given "error" string hard-coded. return "error" or nil.

def log_file(file)   file.open(file).each_line |line|     return "error" if line =~ /error/i   end   nil end 

or true/false

def success?(file)   file.open(file).each_line |line|     return false if line =~ /error/i   end end  if success?(file)   # no error else   # error end 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -