Skip to content

Instantly share code, notes, and snippets.

@mimosz
Created January 19, 2012 09:52
Show Gist options
  • Save mimosz/1639114 to your computer and use it in GitHub Desktop.
Save mimosz/1639114 to your computer and use it in GitHub Desktop.
帮着优化优化呗~~
def show_props(str)
props = str.split(';') # 切割
if props.is_a?(Array)
str = "<dl class='props'>"
props.each do |property|
prop_arr = property.split(':') # 切割
if prop_arr.is_a?(Array) && prop_arr.count == 4
str += "<dt title='#{prop_arr[0]}'>#{prop_arr[2]}</dt><dd title='#{prop_arr[1]}'>#{prop_arr[3]}</dd>"
else
str += "<dt class='warning'>提示:</dt><dd>无属性</dd>"
end
end
str += "</dl>"
end
str
end
@LeoShi
Copy link

LeoShi commented Jan 28, 2012

def show_props(str)
    props = str.split(';') # 切割
    str = build_dl props if props.is_a?(Array)
    str
end

 def build_dl props
    str = "<dl class='props'>"
    props.each do |property|
     str += build_dt property
    end
    str += "</dl>"
    str
 end

 def build_dt property
    prop_arr = property.split(':') # 切割
    if prop_arr.is_a?(Array) && prop_arr.count == 4
     str += "<dt title='#{prop_arr[0]}'>#{prop_arr[2]}</dt><dd title='#{prop_arr[1]}'>#{prop_arr[3]}</dd>"
    else
     str += "<dt class='warning'>提示:</dt><dd>无属性</dd>"
    end
    str
 end

Weibo follow me @史磊LeoShi

@Jeweller-Tsai
Copy link

Array#split => # an array 所以应该不需要再判断是不是数组了吧
另外,像prop_arr[0]这种 看了完全不知所云啊 能不能有更好的办法呢
其实在view里面使用helper一点也不OO啊 建议看看draper

@mimosz
Copy link
Author

mimosz commented Feb 15, 2012

split后未必能切出数组来~~

@Jeweller-Tsai
Copy link

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-split
能举个例子吗 官方文档上是写着返回数组的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment