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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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
gabordemeter <gabordemeter-YbzV19J7KiEAvxtiuMwx3w@public.gmane.org>
wrote:>
> 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
>
>
> >
--
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 On Jun 20, 1:30 pm, gabordemeter <gabordeme...-YbzV19J7KiEAvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 20, 2007, at 4:34 PM, Tyler MacDonald wrote:> 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 > > gabordemeter <gabordemeter-YbzV19J7KiEAvxtiuMwx3w@public.gmane.org> wrote: >> 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, >> >> Gabor91..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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jun-20 20:48 UTC
Re: case statement ranges
Hi -- On Wed, 20 Jun 2007, gabordemeter wrote:> > 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?The .. is inclusive; the ... is exclusive. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...
On Wed, 20 Jun 2007, gabordemeter wrote:
>
> 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
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
>> 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 :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---