HAML .each函数用于变量 [英] HAML .each function for variables

查看:84
本文介绍了HAML .each函数用于变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有30多个变量,其中包含一个数组,数组中包含多个字符串.

I have around 30+ variables that contain an array with multiple strings inside of it.

1个变量= 1个数组.

1 variable = 1 array.

我想知道是否有可能创建一个包含所有变量名称的新变量,以便我可以遍历它们.

I want to know if it is possible to create a new variable that will contain all the variables names, so i can loop through them.

例如:

  1. 这些是单个数组:

  1. These are the individual arrays:

- @a = ["a","b","c","d","d"];
- @b = ["a","b","c","c","d"];
- @c = ["a","b","c","d"];

  • 现在,我想在单独的变量中获取所有唯一和重复的字符串,如下所示:

  • Now i want to get all the unique and duplicate strings in separate variables, like this:

    - @x_uniq = @a.uniq
    - @x_dup = @a.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }.select{ |k,v| v > 1 }.keys 
    ...
    - @x_uniq = @b.uniq
    - @x_dup = @b.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }.select{ |k,v| v > 1 }.keys 
    ...
    - @x_uniq = @c.uniq
    - @x_dup = @c.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }.select{ |k,v| v > 1 }.keys 
    

  • 这是我使用每个x_uniq和x_dup变量的方式:

  • This is how i use each x_uniq and x_dup variable:

    - @x_uniq = @a.uniq
    - @x_dup = @a.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }.select{ |k,v| v > 1 }.keys 
    
    %ul.list-inline
     - @x_uniq.each do |x|
       %li            
         %i{:class=>"fas fa-#{x} fa-2x"}
         %span.f6 #{x}                      
     - @x_dup.each do |x|
       %li            
         %i{:class=>"fas fa-#{x} fa-2x"}
         %span.f6 #{x} 
    

  • 如果我有30个带有数组的变量,则需要分别为每个变量做一个.each()循环.我想知道,如果有人知道一种方法来遍历包含不同数据的30个x_uniq和x_dup变量.

    我当时正在考虑创建一个包含所有3个变量的变量.像这样(顺便说一句,我不知道变量@d是否正确):

    I was thinking to create an variable that will contain all the 3 variables. Something like this (btw, i don't know if the variable @d is correct):

    - @d = [@a,@b,@c];
    

    我接下来要做的是遍历@d变量,以获取各个变量及其内容. 问题是我不知道如何遍历@d变量.

    What i want to do next is to iterate through the @d variable, to get the individual variables and their contents. The problem is that i don't know how to iterate through the @d variable.

    有人对我的问题有解决方案吗?还是解决此问题的新方法?

    Does someone has a solution for my issue? Or a new way of approaching this matter?

    谢谢.

    推荐答案

    不要创建其他不相关的变量.始终尝试从操纵结构的角度考虑您的Ruby代码:

    Don't create piles of otherwise unrelated variables. Always try and think about your Ruby code in terms of manipulating structures:

    - @data = { a: ["a","b","c","d","d"], b: ["a","b","c","c","d"], c: ["a","b","c","d"] }
    

    然后定义一个方法,该方法采用该数组并返回中断的唯一数据和重复数据消除的数据:

    Then define a method that takes that array and returns the broken out unique and and de-duplicated data:

    def dedup_uniq(array)
      {
        uniq: array.uniq,
        dup: array.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }.select { |k,v| v > 1 }.keys
      }
    end
    

    然后轻松地进行处理,您只需进行迭代即可:

    Then processing this is easy, you just iterate:

    - @data = @data.map { |k, d| [ k, dedup_uniq(d) ] }.to_h
    

    然后您具有所需的结构:

    Then you have the structure you want:

    - @data.each do |k, d|
      %ul.list-inline
        - d[:uniq].each do |x|
        %li            
          %i{:class=>"fas fa-#{x} fa-2x"}
          %span.f6 #{x}                      
      - d[:dup].each do |x|
        %li            
          %i{:class=>"fas fa-#{x} fa-2x"}
          %span.f6 #{x}
    

    这篇关于HAML .each函数用于变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆