>> 'ds.35bdg56'[/\d+/] => "35"
或者,因为你确实要求一个函数…
$irb >> def f x; x[/\d+/] end => nil >> f 'ds.35bdg56' => "35"
你可以真的有一些乐趣与此:
>> class String; def firstNumber; self[/\d+/]; end; end => nil >> 'ds.35bdg56'.firstNumber => "35"