Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kellabyte
Created November 8, 2015 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellabyte/8cdde757e3be24d61a91 to your computer and use it in GitHub Desktop.
Save kellabyte/8cdde757e3be24d61a91 to your computer and use it in GitHub Desktop.

Why does this work on Rubular.com but not in Ruby code? What am I doing wrong?

Source string

Running 1s test @ http://192.168.0.2:8000\n  8 threads and 256 connections\n  Thread Stats   Avg      Stdev     Max   +/- Stdev\n    Latency     2.08ms    3.91ms  61.86ms   92.89%\n    Req/Sec    23.80k    10.45k   60.63k    74.70%\n  Latency Distribution\n     50%    1.10ms\n     75%    1.59ms\n     90%    4.03ms\n     99%   22.04ms\n  197510 requests in 1.10s, 29.76MB read\nRequests/sec: 179561.40\nTransfer/sec:     27.06MB

Ruby regex

output.match("Requests\/sec: (.*)\\n")

Result

Requests/sec: 176706.95\nTransfer/sec:     26.63MB", "warnings": []}

If I test it on http://rubular.com I get (which is what I want):

179561.40
@zph
Copy link

zph commented Nov 8, 2015

<MatchData "Requests/sec: 179561.40\n" 1:"179561.40"> is the response from your match.

So output.match("Requests\/sec: (.*)\\n")[1] gives you the content of first capture.

Pry repl and seeing_is_believing.gem can be super helpful for exploring libraries or interfaces in Ruby.

@davefp
Copy link

davefp commented Nov 9, 2015

I get the same result as @zph. Can you post the full code that leads to your result?

My IRB output is below. I'm using Ruby 2.1.1p76

irb(main):001:0> str = "Running 1s test @ http://192.168.0.2:8000\n  8 threads and 256 connections\n  Thread Stats   Avg      Stdev     Max   +/- Stdev\n    Latency     2.08ms    3.91ms  61.86ms   92.89%\n    Req/Sec    23.80k    10.45k   60.63k    74.70%\n  Latency Distribution\n     50%    1.10ms\n     75%    1.59ms\n     90%    4.03ms\n     99%   22.04ms\n  197510 requests in 1.10s, 29.76MB read\nRequests/sec: 179561.40\nTransfer/sec:     27.06MB"
=> "Running 1s test @ http://192.168.0.2:8000\n  8 threads and 256 connections\n  Thread Stats   Avg      Stdev     Max   +/- Stdev\n    Latency     2.08ms    3.91ms  61.86ms   92.89%\n    Req/Sec    23.80k    10.45k   60.63k    74.70%\n  Latency Distribution\n     50%    1.10ms\n     75%    1.59ms\n     90%    4.03ms\n     99%   22.04ms\n  197510 requests in 1.10s, 29.76MB read\nRequests/sec: 179561.40\nTransfer/sec:     27.06MB"
irb(main):002:0> str
=> "Running 1s test @ http://192.168.0.2:8000\n  8 threads and 256 connections\n  Thread Stats   Avg      Stdev     Max   +/- Stdev\n    Latency     2.08ms    3.91ms  61.86ms   92.89%\n    Req/Sec    23.80k    10.45k   60.63k    74.70%\n  Latency Distribution\n     50%    1.10ms\n     75%    1.59ms\n     90%    4.03ms\n     99%   22.04ms\n  197510 requests in 1.10s, 29.76MB read\nRequests/sec: 179561.40\nTransfer/sec:     27.06MB"
irb(main):003:0> regex = "Requests\/sec: (.*)\\n"
=> "Requests/sec: (.*)\\n"
irb(main):004:0> str.match(regex)
=> #<MatchData "Requests/sec: 179561.40\n" 1:"179561.40">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment