Just readable variant:
f.read.gsub( /^(A000[0-9]{10})\s*=\s*(.*)/ ) do | _ |
values = $2.split( ',' )
values[ 6 ] = new_value
"A000#{$1} = #{values.join( ',' )}"
end
_
means, that the argument is passed, but isn't used in a fucntion or block, please refer to the good code standart. _
shell be used, when the block strongly requires the explicitly declared argument, and other way, for example, in the code above, it is not needed, and could be just omitted. Also for ruby above equal 1.9 you can use the named group argument for a Regexp
, so the code could be optimized as follows:
f.read.gsub( /^(?<key>A000[0-9]{10})\s*=\s*(?<valueset>.*)/ ) do
values = valueset.split( ',' )
values[ 6 ] = new_value
"A000#{key} = #{values.join( ',' )}"
end