HAML .each function for variables

牧云@^-^@ 提交于 2020-01-06 05:30:29

问题


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

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.

For example:

  1. These are the individual arrays:

    - @a = ["a","b","c","d","d"];
    - @b = ["a","b","c","c","d"];
    - @c = ["a","b","c","d"];
    
  2. 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 
    
  3. 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} 
    
  4. If i have 30 variables with arrays, i need to do an .each() loop for each of them individually. I want to know, if someone knows a way to iterate through the 30 x_uniq and x_dup variables, that contain different data.

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];

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?

Thank you.


回答1:


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}


来源:https://stackoverflow.com/questions/52105788/haml-each-function-for-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!