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

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -