Skip to content

Latest commit

 

History

History
25 lines (20 loc) · 484 Bytes

get_number_from_string.md

File metadata and controls

25 lines (20 loc) · 484 Bytes

Description

Write a function which removes from string all non-digit characters and parse the remaining to number. E.g: "hell5o wor6ld" -> 56

Function:

get_number_from_string(s)

My Solution

def get_number_from_string(s)
  s.gsub(/[^0-9]/, '').to_i
end

Better/Alternative solution from Codewars

def get_number_from_string(s)
  s.scan(/\d+/).join.to_i
end