Comparison of Array with Array failed

I am passing this hash to a method to sort it, which works in irb but in
rails I can’t get past it

report = {
:cals => @total_cals,
:fat => @dv_fat,
:sfat => @dv_sfat,
:carbs => @dv_carbs,
:chol => @dv_chol,
:sodium => @dv_sodium,
:protein => @dv_protein,
:fiber => @dv_fiber,
:calcium => @dv_calcium,
:copper => @dv_copper,
:folid_acid => @dv_folid_acid,
:iron => @dv_iron,
:magnesium => @dv_magnesium,
:iron => @dv_iron,
:pantothenic_acid => @dv_pantothenic_acid,
:phosphorus => @dv_phosphorus,
:riboflavin => @dv_riboflavin,
:thiamin => @dv_thiamin,
:a_iu => @dv_a_iu,
:b6 => @dv_b6,
:b12 => @dv_b12,
:c => @dv_c,
:d => @dv_d,
:e => @dv_e,
:zinc => @dv_zinc,
:potassium => @dv_potassium }

Here is the method giving me trouble

def overview_text(report_hash)

array_sorted = report_hash.sort { |a,b| a[1] <=> b[1] }.reverse

rich = very_plentiful = plentiful = good_source =
small_amount = minimal_amount = Array.new

@a = 0
until @a == array_sorted.length
  if array_sorted[@a].last > 35 && (array_sorted[@a][0] == :fat or

array_sorted[@a][0] == :carbs or array_sorted[@a][0] == :protein or
array_sorted[@a][0] == :fiber or array_sorted[@a][0] == :chol)
rich << array_sorted[@a].first.to_s
elsif array_sorted[@a].last > 35 && (array_sorted[@a][0] != :fat
or array_sorted[@a][0] != :carbs or array_sorted[@a][0] != :protein or
array_sorted[@a][0] != :fiber or array_sorted[@a][0] != :chol)
very_plentiful << array_sorted[@a].first.to_s
elsif array_sorted[@a].last <= 35 && array_sorted[@a].last > 20
plentiful << array_sorted[@a].first.to_s
elsif array_sorted[@a].last <= 20 && array_sorted[@a].last > 8
good_source << array_sorted[@a].first.to_s
elsif array_sorted[@a].last <= 8 && array_sorted[@a].last > 2
small_amount << array_sorted[@a].first.to_s
elsif array_sorted[@a].last <= 2 && array_sorted[@a].last > 0
minimal_amount << array_sorted[@a].first.to_s
end
@a += 1
end
output = "Rich in #{rich.to_sentence}, ",
"Very Plentiful in #{very_plentiful.to_sentence}, ",
"Plentiful of #{plentiful.to_sentence}, ",
"a Good Source of #{good_source.to_sentence}, ",
"with Small Amounts of #{small_amount.to_sentence}, ",
“and Very Low in #{minimal_amount.to_sentence}.”
.
.
.
end

Jesse C. wrote:

rich = very_plentiful = plentiful = good_source =
small_amount = minimal_amount = Array.new

You are creating one array and assigning references to it to all your
variables. Everything you do to any of them, is done to all of them,
because they all point to one array. This is not what you intended to
do, I suppose. Make each assignment in a separate line.

TPR.

**your solution solves the problem :slight_smile:

I fixed the problem by passing a ternary operator to the assignments in
the hash.

And also I just realized that your solution had been a part of the
problem, if not the whole problem, and fixed it just before reading your
solution. Maybe you sent it via karma, and I’m just learning a little
more here :slight_smile:

Thomas B. wrote:

Jesse C. wrote:

rich = very_plentiful = plentiful = good_source =
small_amount = minimal_amount = Array.new

You are creating one array and assigning references to it to all your
variables. Everything you do to any of them, is done to all of them,
because they all point to one array. This is not what you intended to
do, I suppose. Make each assignment in a separate line.

TPR.