如何根据数据键将数据值排列到数组中 - Ruby

时间:2016-02-13 21:32:25

标签: arrays ruby sorting

我的模型中有一些数据(在一个名为results的变量中),我想根据它们的键将它组织成数组。 I.E,每个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中完成这个。

修改

将所有对象值添加到Hash中可能会更有意义,但我担心唯一的键名...

2 个答案:

答案 0 :(得分:0)

String TIME_PATTERN = "^\\d\\d:\\d\\d\\s[AP]M$";

final JTextField tf = new JTextField("00:00 AM", 8);

((AbstractDocument)tf.getDocument()).setDocumentFilter(new DocumentFilter() {
    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {

        String text = fb.getDocument().getText(0, fb.getDocument().getLength());

        text = text.substring(0, offs) + str + text.substring(offs + length);

        if(text.matches(TIME_PATTERN)) {
            super.replace(fb, offs, length, str, a);
            return;
        }

        text = fb.getDocument().getText(0, fb.getDocument().getLength());

        if(offs == 2 || offs == 5)
            tf.setCaretPosition(++offs);

        if(length == 0 && (offs == 0 ||offs == 1 ||offs == 3 ||offs == 4 ||offs == 6))
            length = 1;

        text = text.substring(0, offs) + str + text.substring(offs + length);

        if(!text.matches(TIME_PATTERN))
            return;

        super.replace(fb, offs, length, str, a);

    }

    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException { }

    public void remove(FilterBypass fb, int offset, int length) throws BadLocationException { }

});

这将迭代结果中的每个对象,并返回一个包含每个genreName字段的数组,只包含类&#34;类型&#34;的对象。然后,您可以执行与其他字段类似的操作。

答案 1 :(得分:0)

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

genre_names = results.map { |object| object.genreName if object.class.name == "Genre" }

你可以这样做:

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=>[]}

等等。