开发者

Beginner refactor question

开发者 https://www.devze.com 2023-03-23 13:21 出处:网络
I would like to know the best or most correct way to refactor this. series = Hash.new seri开发者_StackOverflowes[:series_id] = doc.xpath(\"Data/Series/id\").inner_text if not doc.xpath(\"Data/Series/

I would like to know the best or most correct way to refactor this.

series = Hash.new
seri开发者_StackOverflowes[:series_id] = doc.xpath("Data/Series/id").inner_text if not doc.xpath("Data/Series/id").nil?


series = { :series_id => doc.xpath("Data/Series/id").try(:inner_text) }


Something like

series = Hash.new
id_element = doc.xpath("Data/Series/id")
series[:series_id] =  id_element.inner_text unless id_element.nil?


I think the most revolutionary refactoring would be using xml to object mapping library like happymapper and leave all this dirty implementation details under the hood.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号