Issue #21932 has been updated by mame (Yusuke Endoh). Here is a supplement to Matz's decision. This method will basically follow the behavior of `String#to_i`. The base can be specified as the second argument: ```ruby "2024" =~ /(\d+)/ $~.integer_at(1) # => 2024 (default: base 10) $~.integer_at(1, 8) # => 1044 (interprets "2024" as base 8) $~.integer_at(1, 16) # => 8228 (interprets "2024" as base 16) ``` When it encounters non-numeric characters or an empty string, it behaves the same as `String#to_i`: ```ruby # integer_at should behave as String#to_i "foo" =~ /(...)/ $~.integer_at(1) # => 0 (== "foo".to_i) "0xF" =~ /(...)/ $~.integer_at(1) # => 0 (== "0xF".to_i, not 15) "" =~ /(\d*)/ $~.integer_at(1) # => 0 (== "".to_i) "1_0_0" =~ /(\d+(?:_\d+)*)/ $~.integer_at(1) # => 100 (== "1_0_0".to_i) ``` If the base is set to 0, it respects prefixes like `0x` (the same as `String#to_i(0)`): ```ruby "0xF" =~ /(...)/ $~.integer_at(1, 0) # => 15 (== "0xF".to_i(0)) ``` If there is no match for the group, it returns `nil`: ```ruby "b" =~ /(a)|(b)/ $~.integer_at(1) # => nil ``` ---------------------------------------- Feature #21932: `MatchData#get_int` https://bugs.ruby-lang.org/issues/21932#change-116768 * Author: nobu (Nobuyoshi Nakada) * Status: Open ---------------------------------------- This is suggested by @akr today, `$~.get_int(1)` is equivalent to `$1.to_i` but does not create the intermediate string object. https://github.com/nobu/ruby/tree/match-get_int -- https://bugs.ruby-lang.org/