Case statement anomaly?

This works:

language_name = "C"

case language_name
  when "C"
    puts "The language is C."
  when "Rexx"
    puts "The language is Rexx."
  else
    puts "The language is unknown"
end

So does this:

case language_name
  when "C"
    begin
      puts "The language is C."
    end
  when "Rexx"
    begin
      puts "The language is Rexx."
    end
  else
    begin
      puts "The language is unknown."
    end
end

But this gives a syntax error:

case language_name
  when "C"
    do
      puts "The language is C."
    end
  when "Rexx"
    do
      puts "The language is Rexx."
    end
  else
    do
      puts "The language is unknown."
    end
end

Why? Nowhere I looked seemed to help. What am I missing?

I know do/end isn't necessary, but why does it fail?

Thanks.

···

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

Paul, the "begin ... end" syntax is meant for exception handling. For
example, you can use it like this:

begin
  a = 1/0
rescue ZeroDivisionError
  puts "You tried to divide by zero!"
end

The "do ... end" syntax is used for blocks. The "when" part in a case
statement doesn't accept a block argument (it's not even a method), so it
is a syntax error.

···

-----
Carlos Agarie

Control engineering
Polytechnic School, University of São Paulo, Brazil
Computer engineering
Embry-Riddle Aeronautical University, USA

2012/12/27 Paul Magnussen <lists@ruby-forum.com>

This works:

language_name = "C"

case language_name
  when "C"
    puts "The language is C."
  when "Rexx"
    puts "The language is Rexx."
  else
    puts "The language is unknown"
end

So does this:

case language_name
  when "C"
    begin
      puts "The language is C."
    end
  when "Rexx"
    begin
      puts "The language is Rexx."
    end
  else
    begin
      puts "The language is unknown."
    end
end

But this gives a syntax error:

case language_name
  when "C"
    do
      puts "The language is C."
    end
  when "Rexx"
    do
      puts "The language is Rexx."
    end
  else
    do
      puts "The language is unknown."
    end
end

Why? Nowhere I looked seemed to help. What am I missing?

I know do/end isn't necessary, but why does it fail?

Thanks.

--
Posted via http://www.ruby-forum.com/\.

begin
  puts "The language is C."
end

--output:--
The language is C

do
  puts "The language is C."
end

--output:--
1.rb:1: syntax error, unexpected keyword_do_block
1.rb:3: syntax error, unexpected keyword_end, expecting $end

···

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

Thank you both.

Carlos, where is this information on the 'Net, please? I'm afraid I
couldn't find it, although I looked at ruby-lang.org and several other
places.

···

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

Carlos, where is this information (about 'when' not accepting a block)
on the 'Net, please? I'm afraid I couldn't find it, although I looked
at ruby-lang.org and several other places.

Well you have to think out that every code inside "when" and the next
"when" ;or between "when" and "else";or between "when" and "end";or
between "else" and "end" is a chunk of code, so is like a block, you
don't need to put the code inside "do .. end" or {}. It is not necesary,
of course you can use blocks inside for example:

a = 1
b = ["one", "two"]

case a
when 0
   b.each do │number│
     number.capitalize!
   end
else
   begin
    a.length
   rescue => e
     puts "The error is #{e}"
   end
end

This will finish with an error, and I maked that with the purpose to
show in which case you can use "begin .. end", if you don't want to
rescue nothing is not necesary use "begin .. end", just put the chunk of
code. Well hope this help you, see ya' :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.