Here is the line of code:
options[:selectors] = [[1, "Spain"], [2, "Italy"]]
v = "Monthly"
value = Hash[options[:selectors]][!!(v =~ /^[0-9]+$/) ? v.to_i : v]
The main part I’m not sure of is this part of the last line:
[!!(v =~ /^[0-9]+$/) ? v.to_i : v]
What does [!! mean, and what does =~ mean? Lastly, what’s the overall line mean?
!!is an idiom that means “change this value into a boolean.” The first!does the type conversion and the second!makes reverts the semantic meaning while keeping it a boolean.This is not really appropriate in ruby, though. I believe it is a PHPism.
=~is a regex match.Finally, the whole thing means “if v is an int string, make v an integer type and fetch from the hash, otherwise use the string as the key.”