(Ruby) Is there a function to easily find the first number in a string?

ruby, string

Solution

>>  'ds.35bdg56'[/\d+/]
=> "35"

Or, since you did ask for a function...

$ irb
>> def f x; x[/\d+/] end
=> nil
>> f 'ds.35bdg56'
=> "35"

You could really have some fun with this:

>> class String; def firstNumber; self[/\d+/]; end; end
=> nil
>> 'ds.35bdg56'.firstNumber
=> "35"

Problem

For example, if I typed "ds.35bdg56" the function would return 35. Is there a pre-made function for something like that or do I need to iterate through the string, find the first number and see how long it goes and then return that?

Original source