【问题标题】:How to arrange data values into arrays based on their key - Ruby如何根据键将数据值排列到数组中 - Ruby
【发布时间】:2016-02-13 21:32:25
【问题描述】:

我从我的模型中获得了一些数据(在一个名为 results 的变量中),我想根据它们的键将它组织成数组。即,每个genreName 值都应该被推入genres[] 数组,每个actorID 应该被推入cast[] 数组。

results

#<Genre genreName: "Fantasy">
#<Genre genreName: "Comedy">
#<Genre genreName: "Children">
#<Genre genreName: "Animation">
#<Genre genreName: "Adventure">
#<Actor actorID: "tom_hanks", actorName: "Tom Hanks">
#<Actor actorID: "tim_allen", actorName: "Tim Allen">
#<Actor actorID: "don_rickles", actorName: "Don Rickles">
#<Actor actorID: "jim_varney", actorName: "Jim Varney">
#<Actor actorID: "wallace_shawn", actorName: "Wallace Shawn">
#<Director directorID: "john_lasseter", directorName: "John Lasseter">
#<Country countryName: "USA">
#<Location locationName: "N/A">

你有什么建议?我正在尝试在 Ruby 中完成此操作。

编辑

也许将所有对象值添加到哈希中会更有意义,但我担心唯一的键名......

【问题讨论】:

  • 您应该将results 显示为有效的Ruby 对象。假设它是一个字符串数组:results = ["#&lt;Genre genreName: "Fantasy"&gt;, ..."#&lt;Location locationName: "N/A"&gt;"].
  • ...如果results 是一个类实例数组,那需要说明。

标签: arrays ruby sorting


【解决方案1】:
genre_names = results.map { |object| object.genreName if object.class.name == "Genre" }

这将遍历结果中的每个对象,并通过仅包含“流派”类的对象返回一个包含每个流派名称字段的数组。然后,您可以对其他字段执行类似操作。

【讨论】:

    【解决方案2】:

    (编辑:我可能误解了这个问题。我将数据视为字符串数组,但它们似乎更有可能是类实例。)

    results = <<_.lines
    #<Genre genreName: "Fantasy">
    #<Genre genreName: "Comedy">
    #<Genre genreName: "Children">
    #<Genre genreName: "Animation">
    #<Genre genreName: "Adventure">
    #<Actor actorID: "tom_hanks", actorName: "Tom Hanks">
    #<Actor actorID: "tim_allen", actorName: "Tim Allen">
    #<Actor actorID: "don_rickles", actorName: "Don Rickles">
    #<Actor actorID: "jim_varney", actorName: "Jim Varney">
    #<Actor actorID: "wallace_shawn", actorName: "Wallace Shawn">
    #<Director directorID: "john_lasseter", directorName: "John Lasseter">
    #<Country countryName: "USA">
    #<Location locationName: "N/A">
    _
    

    你可以这样做:

    R = /
        \b        # Match a word break
        (genreName|actorID|cast) # Match one of three strings in capture group 1
        \b        # Match a word break 
        (?=       # Begin a positive lookahead 
          :\s+\"  # Match : >= 1 whitespace double quote
          (\w+)   # Match >= 1 word characters in capture group 2
          \"      # Match double quote
        )         # End postive lookahead
        /x        # Extended/free-spacing regex definition mode
    
    
    h = results.each_with_object({ genreName: [], actorID: [], cast: [] }) { |s,h|
      s.scan(R) { h[$1.to_sym] << $2 } }
      #=> {:genreName=>["Fantasy", "Comedy", "Children", "Animation", "Adventure"],
      #    :actorID=>["tom_hanks", "tim_allen", "don_rickles", "jim_varney", "wallace_shawn"],
      #    :cast=>[]}
    

    然后

    actorID = h[:actorID]
    

    等等。

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多