【问题标题】:Creating a simple array with multiple values in Ruby on Rails在 Ruby on Rails 中创建具有多个值的简单数组
【发布时间】:2012-09-16 00:06:17
【问题描述】:

在我使用过的每种语言中,数组一直是我的失败之处,但我确实需要在 Rails 中创建多个项目的动态数组(注意 - 这些都与模型无关)。

简而言之,数组的每个元素都应包含 3 个值 - 一个单词、它的语言和翻译成英文。例如,这是我想做的:

myArray = Array.new

然后我想将一些值推送到数组中(注意 - 实际内容取自其他地方 - 尽管不是模型 - 并且需要通过循环添加,而不是像这里那样硬编码):

myArray[0] = [["bonjour"], ["French"], ["hello"]]

myArray[1] = [["goddag"], ["Danish"], ["good day"]]

myArray[2] = [["Stuhl"], ["German"], ["chair"]]

我想创建一个循环来在一行中列出每个项目,如下所示:

<ul>
<li>bonjour is French for hello</li>
<li>goddag is Danish for good day</li>
<li>Stuhl is German for chair</li>
</ul>

但是,我正在努力 (a) 弄清楚如何将多个值推送到单个数组元素,以及 (b) 我将如何循环并显示结果。

不幸的是,我并没有走得太远。我似乎无法弄清楚如何将多个值推送到单个数组元素(通常发生的是 [] 括号包含在输出中,我显然不想要 - 所以它可能是一个符号错误)。

我应该改用哈希吗?

目前,我有三个独立的数组,这是我一直在做的,但我不是特别喜欢 - 即一个数组保存原始单词,一个数组保存语言,一个保存翻译的最终数组。虽然它有效,但我确信这是一种更好的方法 - 如果我能解决的话!

谢谢!

【问题讨论】:

    标签: ruby-on-rails arrays loops multidimensional-array


    【解决方案1】:

    好的,假设您在 CSV 文件中有想要的字词:

    # words.csv
    bonjour,French,hello
    goddag,Danish,good day
    stuhl,German,chair
    

    现在在我们的程序中,我们可以执行以下操作:

    words = []
    File.open('words.csv').each do |line|
      # chomp removes the newline at the end of the line
      # split(',') will split the line on commas and return an array of the values
      # We then push the array of values onto our words array
      words.push(line.chomp.split(','))
    end
    

    执行此代码后,words 数组中有三个项目,每个项目都是一个基于我们文件的数组。

    words[0]  # => ["bonjour", "French", "hello"]
    words[1]  # => ["goddag", "Danish", "good day"]
    words[2]  # => ["stuhl", "German", "chair"]
    

    现在我们要显示这些项目。

    puts "<ul>"
    words.each do |word|
      # word is an array, word[0], word[1] and word[2] are available
      puts "<li>#{word[0]} is #{word[1]} for #{word[2]}</li>"
    end
    puts "</ul>"
    

    这给出了以下输出:

    <ul>
    <li>bonjour is French for hello</li>
    <li>goddag is Danish for good day</li>
    <li>stuhl is German for chair</li>
    </ul>
    

    另外,你没有问过这个问题,但你可以使用以下方法访问给定数组的一部分:

    words[0][1]  # => "French"
    

    这告诉 ruby​​ 你想查看 words 数组的第一个(Ruby 数组是从零开始的)元素。 Ruby 找到那个元素 (["bonjour", "French", "hello"]) 并看到它也是一个数组。然后,您要求输入该数组的第二项 ([1]),Ruby 返回字符串“French”。

    【讨论】:

    • 另外,语法 words = [] 和 words = Array.new 是等价的。
    • 谢谢!这给了我一些线索。然而,不幸的是,内容来自不同的来源 - 没有什么比 CSV 文件更整洁!但这是一个开始 - 干杯!
    • CSV 文件只是一个示例。如果您有一组单词、语言、翻译,您可以使用 myArray.push(...) 将其推送到 myArray 中。
    【解决方案2】:

    你的意思是这样的?

    myArray.map{|s|"<li>#{[s[0],'is',s[1],'for',s[2]].join(" ")}</li>"}
    

    【讨论】:

      【解决方案3】:

      感谢你们的帮助!根据您的建议,我设法找到了解决方案

      为了其他偶然发现此问题的人的利益,这是我省略的代码。注意:我使用了三个变量,称为文本、语言和翻译,但我想您可以将它们替换为具有三个独立元素的单个数组,正如 Jason 上面所建议的那样。

      在控制器中(通过循环添加内容):

      #loop start
      
      my_array.push(["#{text}", "#{language}", "#{translation}"]) 
      
      #loop end
      

      在视图中:

      <ul>
      
      <% my_array.each do |item| %>
      
      <li><%= item[0] # 0 is the original text %> is 
      <%= item[1] # 1 is the language %> for 
      <%= item[2] # 2 is the translation %></li>
      
      <% end %>
      
      </ul>
      

      再次感谢!

      【讨论】:

        猜你喜欢
        • 2011-09-25
        • 2012-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多