Created
July 15, 2013 21:33
-
-
Save bplexico/6003695 to your computer and use it in GitHub Desktop.
catch throw refactor
This file contains 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
# OLD | |
def insert_uncategorized_category(uncategorized) | |
insert_at = nil | |
@categories.each_with_index do |c, idx| | |
if (uncategorized.topics_week || 0) > (c.topics_week || 0) | |
insert_at = idx | |
break | |
end | |
end | |
@categories.insert(insert_at || @categories.size, uncategorized) | |
end | |
# NEW | |
def insert_uncategorized_category(uncategorized) | |
insert_at = catch(:idx){ | |
@categories.each_with_index do |c, idx| | |
throw(:idx, idx) if (uncategorized.topics_week || 0) > (c.topics_week || 0) | |
end | |
nil # <-- return nil if we get this far | |
} | |
@categories.insert(insert_at || @categories.size, uncategorized) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment