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

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -