HAML .each Function For Variables
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 con
Solution 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}
Post a Comment for "HAML .each Function For Variables"