Created
September 15, 2020 15:50
-
-
Save amitpatelx/2d5074203a06703dba63e94c4139ac91 to your computer and use it in GitHub Desktop.
Stop creating Array in method which is called multiple times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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