or even better can I do hash.has_key?('videox') where x is
- ” nothing or
- a digit?
so ‘video’, ‘video1’, ‘video2’ would pass the condition?
of course I can have two conditions but in case I need to use video3 in the future things would get more complicated…
If you want the general case of video followed by a digit without explicitly listing all the combinations there are a couple of methods from Enumerable that you could use in combination with a regular expression.
hash.keysis an array of the keys fromhashand^video\d$matches video followed by a digit.or
grepwould also allow you to capture the matching key(s) if you needed that information for the next bit of your code e.g.Furthermore, if the prefix of key we’re looking for is in a variable rather than a hard coded string we can do something along the lines of: