0

I have a variable that prints like this:

{:type1=>:poor, :type2=>:avg, :type3=>:best}

I want to iterate through this hash and print out a final value based on following rules

  • if value for any of the types is :best then final value is best
  • if value for any of the types is :avg and there are no best then final value is avg
  • otherwise final value is poor

basically the strongest ones wins.

I've tries the following

def final_value(values)
  val = "poor"
  values.each do |key, val|

  end
  val
end
2
  • You also have symbols :poor etc, but you wrote as if there are variable/methods poor etc. Then, you are even mentioning strings "poor" etc. Commented Apr 8, 2013 at 17:54
  • I'm simply treating them as strings in my loop even though they are symbols Commented Apr 8, 2013 at 17:54

4 Answers 4

5

Use Hash's has_value? method.

val = :poor;
val = :avg  if values.has_value?(:avg);
val = :best if values.has_value?(:best);
val
Sign up to request clarification or add additional context in comments.

4 Comments

+1 good answer - indeed, iteration is unnecessary! you can slightly improve it by looking for best first, cause then you might not need to look for avg
@alfasin The implementation of the has_value? method does iteration. Iteration is necessary, it's just a matter of explicit or implicit iteration.
@AlexWayne I think that you understood what I meant didn't you ? :) In case not: I was comparing it to cainy393's answer - I meant that it is unnecessary to implement iteration since Ruby does it for you using has_value?. The "implicit" way is more elegant - that's what I meant.
BTW, on a second thought, iteration is actually NOT needed, if the implementation would change to hold an "opposite" Hash (one in which values point to the boolean "true") -it's could be done in O(1). I think that the reason it's not implemented is in order to not waste memory-space, which is a good reason. Just a thought.
0

Some thing like this?

final_val = :poor
values.each() do |val|
    if val = :avg && final_val = :poor
        final_val = :avg
    end
    if val = :best && final_val = :avg
        final_val = :best
    end
end
return final_val

Comments

0

You can achieve what you need as follows:

def final_value(values)
    val = :poor;
    values.each do|k, v|
        if(v==:best)
            val=:best;
            break;
        elsif(val==:poor && v==:avg)
            val=:avg;
        end
    end
    puts "Final value is: "+val.to_s;
end

values = {:type1=>:poor, :type2=>:avg, :type3=>:best};
final_value(values)

Comments

0
def compare(h,val)
 m = ((h.has_value?(val) and val != :poor) ? ((val == :best or h.has_value?(:best))? :best : :avg ) : :poor)
end
h = {:type1=>:poor, :type2=>:avg, :type3=>:best}

p compare(h,:avg) #:best
p compare(h,:trr) #:poor
p compare(h,:best) #:best

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.