[Solved] ruby how to match a substring [closed]


To get an input out of the whole file:

▶ input = input[/PLAY RECAP.*?^(.+?)^localhost/m, 1]

To hashify the result:

▶ input.scan(/(\S+) : ok=(\w+)/).to_h
#⇒ {
#  "ec2-123.compute-1.amazonaws.com" => "16",
#  "ec2-456.compute-1.amazonaws.com" => "11",
#  "ec2-766.compute-1.amazonaws.com" => "40"
# }

To sort by host name (thx Wiktor Stribiżew for the reminder.)

input.scan(/(\S+) : ok=(\w+)/)
     .to_h
     .sort_by { |k, _| k[/(?<=ec2-)\d+/].to_i }

To sort by ok value (whatever it means.)

input.scan(/(\S+) : ok=(\w+)/)
     .to_h
     .sort_by { |_, ok| ok.to_i }

4

solved ruby how to match a substring [closed]