Topic: Switch statement in Ruby?

I'm wondering if Ruby has a switch statement similar to C++ and other languages.  At the moment I'm using 4 'if' statements to check the value of a certain variable.  I know there has to be a better way than using that many 'ifs'.  Does anyone know if Ruby has a switch, or if it has something else that can replace using multiple 'if' statements?

Re: Switch statement in Ruby?

Yep, try this:

case foo
when "bar"
  #..
when "blah"
  #..
else
  #...
end

Railscasts - Free Ruby on Rails Screencasts

Re: Switch statement in Ruby?

Ruby has a case statement (expression, actually):

case foo
  when 1
    puts "Foo is equal to 1"
  when 2..9
    puts "Foo is between 2 and 9"
  when 10
    puts "Foo is equal to 10"
end

Horrible example, but you get the idea. This is a shorter equivelant:

case foo
  when 1    : puts "Foo is equal to 1"
  when 2..9 : puts "Foo is between 2 and 9"
  when 10   : puts "Foo is equal to 10"
end

EDIT: Ryan beats me to it, again! smile

Last edited by fabio (2007-06-20 11:15:12)

Re: Switch statement in Ruby?

Thanks for the replies.  It's exactly what I'm looking for.

Re: Switch statement in Ruby?

The colons are deprecated. I think they will be removed in Ruby 1.9 (or was it 2.0?). Instead there's this:

case foo
  when 1    then puts "Foo is equal to 1"
  when 2..9 then puts "Foo is between 2 and 9"
  when 10   then puts "Foo is equal to 10"
end

Or this:

case foo
  when 1    ; puts "Foo is equal to 1"
  when 2..9 ; puts "Foo is between 2 and 9"
  when 10   ; puts "Foo is equal to 10"
end

Railscasts - Free Ruby on Rails Screencasts

Re: Switch statement in Ruby?

<% case 1 %>
<%     when 1 ;  @display = 'Researching'  %>
<%     else      @display = 'DoingTimepass' %>
<% end %>

I tried this in view template (rhtml) and it throws me syntax errors. Can someone throw light please?

Re: Switch statement in Ruby?

You shouldn't put a case statement in the view like that. Put it in a helper method.

Railscasts - Free Ruby on Rails Screencasts

Re: Switch statement in Ruby?

Is there a way to do something like:

case test
when "cat"
  "meow"
when "dog"
  "woof"
when starts_with?("cow")
  "moo!"
end

so that I could evaluate "cow_12345" and "cowabunga!"?

-patrick

Re: Switch statement in Ruby?

Use a regular expression:

case test
when "cat"
  "meow"
when "dog"
  "woof"
when /^cow/
  "moo!"
end

Re: Switch statement in Ruby?

ryanb wrote:

The colons are deprecated. I think they will be removed in Ruby 1.9 (or was it 2.0?). Instead there's this:

case foo
  when 1    then puts "Foo is equal to 1"
  when 2..9 then puts "Foo is between 2 and 9"
  when 10   then puts "Foo is equal to 10"
end

Or this:

case foo
  when 1    ; puts "Foo is equal to 1"
  when 2..9 ; puts "Foo is between 2 and 9"
  when 10   ; puts "Foo is equal to 10"
end

Really? That is a terrible change.

Re: Switch statement in Ruby?

Really? That is a terrible change.

Well.. I agree using "then" and semicolons is not so nice...  but if you put everything on it's own line then it's very nice... and comma separated items for multi-conditions:

case fruit
  when :papaya, :mango, :passion_fruit, :pineapple
    "tropical"
  when :apple
    "pomaceous"
  when :orange, :grapefruit, :lemon, :lime
    "citrus"
end