Array includes string ruby? -
how check if array includes string element?
address = 'london capital of gb' def get_location(address) all_cities = ['amsterdam','moscow','krakow','london'] city = all_cities.index {|x| x.match /address/ } if city return all_cities[city] else address end end
but returns me full address
i needs return city's name string
use include?
to check whether string contains substring.
address = 'london capital of gb' def get_location(address) all_cities = ['amsterdam','moscow','krakow','london'] city = all_cities.index { |x| address.include? x } if city return all_cities[city] else address end end puts get_location address # output: london
Comments
Post a Comment