how to sort file in ruby -
This is my file content.
Receipt = Power Cobre Pabels = Buy Quentos Pargrar ytd .label = Purchase YTD Verification. Error.maxvalue = value is too large, maximum value {0}
I want to sort this content in alphabetical order ... How can I do this? ?
Update: This code will sort my file.
new_array = File.readlines ("# {$ base_properties}"). Sort file.open ("# {$ Base_properties}", "w") | File | New_array.each {| N | | File.puts (n)} end
Is there a better way to sort the file?
obvious simplification:
new_array = File.readlines ("# {$ Base_properties} "). Sort file. Open ("# {$ base_properties}", "w"). File | File.puts new_array end
I just define a method like this, I'm doing the opposite of the file. Read
. It is extremely reusable, and should actually be a part of the standard:
def file.write! (Path, content) File.open (path, "w") {| Fh | Fh.write content} end
and then trimmed:
file. Write! ($ Base_properties, File.readlines ($ base_properties) .sort)
Comments
Post a Comment