Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Created September 15, 2020 15:50
Show Gist options
  • Save amitpatelx/2d5074203a06703dba63e94c4139ac91 to your computer and use it in GitHub Desktop.
Save amitpatelx/2d5074203a06703dba63e94c4139ac91 to your computer and use it in GitHub Desktop.
Stop creating Array in method which is called multiple times
APARTMENT_TYPES = %w[apartment flat penthouse duplex maisonette]
HOUSE_TYPES = %w[house home villa bungalow detached terraced]
def extract_property_type(details)
return :studio if details.include?('studio')
APARTMENT_TYPES.each do |type|
return :apartment if details.include?(type)
end
HOUSE_TYPES.each do |type|
return :house if details.include?(type)
end
nil
end
def extract_property_type(details)
return :studio if details.include?('studio')
['apartment', 'flat', 'penthouse', 'duplex', 'maisonette'].each do |type|
return :apartment if details.include?(type)
end
['house', 'home', 'villa', 'bungalow', 'detached', 'terraced'].each do |type|
return :house if details.include?(type)
end
nil
end
# This shows that each time you call the method, it creates new array!
def extract_property_type
property_types = ['apartment', 'flat', 'penthouse', 'duplex', 'maisonette']
puts property_types.object_id
end
3.times { extract_property_type }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment