[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

comparison of Array with Array failed

Jesse Crockett

9/21/2008 5:43:00 PM

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}."
5 Answers

TPReal

9/21/2008 6:16:00 PM

0

Jesse Crockett 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.
--
Posted via http://www.ruby-....

Jesse Crockett

9/21/2008 6:52:00 PM

0

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 :-)

Thomas B. wrote:
> Jesse Crockett 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.

--
Posted via http://www.ruby-....

Jesse Crockett

9/21/2008 6:56:00 PM

0

**your solution _solves_ the problem :-)


--
Posted via http://www.ruby-....

Nickname unavailable

12/23/2012 10:22:00 PM

0

On Dec 23, 2:37 pm, Bret Cahill <Bret_E_Cah...@yahoo.com> wrote:
> > which economic theory is more plausible, raising your wages to make
> > paying your debts more easy, or cutting your wages to make your debts
> > harder to pay back?
>
> > i bet "THE CONSERVATIVES" will pick the latter.
>
> Never use logic on tire biters.
>
> It'll just confuse them.
>
> Bret Cahill

i had to cut it down a bit from this,

which economic theory is more plausible, demand for goods and
services is wage driven, or, showering the already filthy rich with
tax cuts, and breaking unions driving up huge wealth inequalities,
will magically sell goods and services?

it seems that was to complicated for them. i am hoping that the new
version is simple enough, if it is not, there is not much left to cut
out to make it any more simple.

Neolibertarian

12/23/2012 11:02:00 PM

0

In article
<07e1b992-69c2-401c-b8a6-8fd4ee851fd3@pp8g2000pbb.googlegroups.com>,
Bret Cahill <Bret_E_Cahill@yahoo.com> wrote:

> > which economic theory is more plausible, raising your wages to make
> > paying your debts more easy, or cutting your wages to make your debts
> > harder to pay back?
> >
> > i bet "THE CONSERVATIVES" will pick the latter.
>
> Never use logic on tire biters.

Porgy?
>
> It'll just confuse them.

Right: tick question. Neither choice is an economic theory, nor
plausible.

Dumb conservatives.

--
Neolibertarian

"Global Warming: It ain't the heat, it's the stupidity."