ruby 树标题(斜线)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 树标题(斜线)相关的知识,希望对你有一定的参考价值。

* [Avoiding redefining constant Struct::ClassName warnings](https://gist.github.com/mungruby/2822135)
require 'pp'

module Categorizer
  def categorize(title_string)
    title = title_string.split('/').reverse
    result = []
    while title.size > 0 do
      x, y = title.shift, title.first
      if x.nil? # parent has nil?
        result << category.new(nil, y)
      else
        result << category.new(y, x)
      end
    end
    result.reverse
  end

  def category
    name = 'Category'
    if Struct::const_defined?(name)
      Struct.const_get(name)
    else
      Struct.new(name, :parent, :me)
    end
  end
end

include Categorizer

title_a = '2014/12/24/daily_report/my_name'
title_b = '2014/12/schedule/01/my_name'

pp categorize(title_a)
puts ''
pp categorize(title_b)

# =>
# [#<struct Struct::Category parent=nil, me="2014">,
#  #<struct Struct::Category parent="2014", me="12">,
#  #<struct Struct::Category parent="12", me="24">,
#  #<struct Struct::Category parent="24", me="daily_report">,
#  #<struct Struct::Category parent="daily_report", me="my_name">]
#
# [#<struct Struct::Category parent=nil, me="2014">,
#  #<struct Struct::Category parent="2014", me="12">,
#  #<struct Struct::Category parent="12", me="schedule">,
#  #<struct Struct::Category parent="schedule", me="01">,
#  #<struct Struct::Category parent="01", me="my_name">]

以上是关于ruby 树标题(斜线)的主要内容,如果未能解决你的问题,请参考以下文章

File.basename

JSP表格画斜线

ruby 如何生成树结构形式的数组?

如何根据 if-then 决策树设置 ruby​​ 变量

ruby Ruby中的节点(树实现)

ruby 重新实现的unix树