I think that the best way to do it would be to use a simple match
operator like:
value = $1 if string =~ /b: (\d+)/
But on a different thread (http://tinyurl.com/38b27) someone explained
that using global variables was a bad habit not to take.
I'm not 100% but as far as I remember $1 is thread local, i.e. no two threads can interfere with each other if they match regular expressions at the same time. In that case it would be save to use $1. Can anybod comment on that?
Thanks for the advice. It's just that it took me two days to remember
that the groups of a pattern are put in $1..$9 with the =~ operator.
I discovered that $1 can have two different values:
- nil if a number is not found
- the number found if it exists
Which means: no errors possible from a previous match. $1 has his value
changed every time.
Wonderful, exactly the "hacker's way" I was looking for.
This feature is not on the (rather old) documentation I was looking at.
I think it's time for me to dig into Ruby's source code...
>
>> value = string[/b: \d+/][/\d+/]
>
> value = string[/b: (\d+)/, 1]
>
> Guy Decoux
Wonderful, exactly the "hacker's way" I was looking for.
This feature is not on the (rather old) documentation I was looking at.
I think it's time for me to dig into Ruby's source code...
You'll find this documented in ri. (Not that I want to discourage you
from looking at the source too