classMarkdownFixer4class<<selfdeffix_all(markdown)methods=[:add_quotes_to_title,:modify_hr_tags,:convert_newlines,:split_tags]methods.inject(markdown){|result,method|self.send(method,result)}enddefadd_quotes_to_title(markdown)markdown.gsub('title','"title"')enddefmodify_hr_tags(markdown)markdown.gsub('hr','hhr')enddefconvert_newlines(markdown)markdown.gsub('\r\n','\n')enddefsplit_tags(markdown)markdown.gsub('tags','t a g s')endendendputsMarkdownFixer4.fix_all('title hr tags\r\n')
This line methods.inject(markdown){|result, method| self.send(method, result) } calls each method in the array passing the result of the previous method the following one, with markdown as the initial value.
This is my favorite one for now :D
This line
methods.inject(markdown){|result, method| self.send(method, result) }
calls each method in the array passing the result of the previous method the following one, withmarkdown
as the initial value.Love these smaller methods.
Easy to read && easy to follow.
Learned some neat refactoring tips-- thanks!
Hmmmmm this one is pretty great! I like the use
inject
a lot. Thanks a lot for the answers! I'm going to think it over on my commute home. :)