case statement ranges

I have the following in my controller:

  def color_coding(value = nil)     case value       when 101..1000000000         "busy_11"

      when 91..100         "busy_10"

      when 81..90         "busy_9"

      when 71..80         "busy_8"

      when 61..70         "busy_7"

      when 51..60         "busy_6"

      when 41..50         "busy_5"

      when 31..40         "busy_4"

      when 21..30         "busy_3"

      when 11..20         "busy_2"

      when 1..10         "busy_1"

      else         "no_class"     end   end

Two questions:

1) when 101..1000000000            "busy_11"

is obviously an ugly hack. What I want is: output "busy_11" if value is 101 or higher. Not sure how I should write this in ruby.

2) when 91..100         "busy_10"

What I want is: output "busy_10" when value is >= 91 and <= 100. Not sure if it should be 91..100 or 91...100 or some other syntax.

Tried to google this but couldn't find a tutorial that covers the above fully. Thanks in advance!

Best,

Gabor

def color_coding(value = nil)   if value && value > 0     if value > 100       "busy_11"     else       "busy_" + ((value/10)+1).to_s     end   end end

  Cheers,     Tyler

Thanks guys! Great stuff!

But one question still remains:

what does 91..100 (two dots between the two numbers) mean, and what does 91...100 (three dots between the two numbers) mean in Ruby?

Best,

Gabor

def color_coding(value = nil)   if value && value > 0     if value > 100       "busy_11"     else       "busy_" + ((value/10)+1).to_s     end   end end

  Cheers,     Tyler

I have the following in my controller:

  def color_coding(value = nil)     case value       when 101..1000000000         "busy_11"

      when 91..100         "busy_10"

      when 81..90         "busy_9"

      when 71..80         "busy_8"

      when 61..70         "busy_7"

      when 51..60         "busy_6"

      when 41..50         "busy_5"

      when 31..40         "busy_4"

      when 21..30         "busy_3"

      when 11..20         "busy_2"

      when 1..10         "busy_1"

      else         "no_class"     end   end

Two questions:

1) when 101..1000000000            "busy_11"

is obviously an ugly hack. What I want is: output "busy_11" if value is 101 or higher. Not sure how I should write this in ruby.

when nil, 0    "no_class" else    "busy_11"

I'm guessing what you might want for value==0

2) when 91..100         "busy_10"

What I want is: output "busy_10" when value is >= 91 and <= 100. Not sure if it should be 91..100 or 91...100 or some other syntax.

Tried to google this but couldn't find a tutorial that covers the above fully. Thanks in advance!

Best,

Gabor

91..100 === x will test (91 <= x && x <= 100)

91...100 === x (three dot range excludes end) will test (91 <= x && x < 100)

What should color_coding(100.5) give?

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Hi --

How about this (untested) instead...

def color_coding(value = nil)    if value >= 101      "busy_11"    elsif value <= 0 || value.nil?      "no_class"    else      "busy_" + (value/10.0).ceil.to_s    end end

Would be a lot shorter...

But one question still remains:

what does 91..100 (two dots between the two numbers) mean, and what does 91...100 (three dots between the two numbers) mean in Ruby?

The .. is inclusive; the ... is exclusive.

Best analogy I've heard is that that extra dot pushes the last number off the cliff where cliff is your range.

That's the only way I can remember it :slight_smile: