Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't "case" with "when > 2" work?

Tags:

ruby

Why is this not working?

case ARGV.length   when 0     abort "Error 1"   when > 2     abort "Error 2" end 
like image 359
emurad Avatar asked Jun 01 '11 23:06

emurad


People also ask

Can you use || IN CASE statement Ruby?

You can't use “||” in case names.

Does Ruby have switch statements?

Ruby uses the case for writing switch statements.

What is case in Ruby?

Case statement in the Ruby is a way to execute different codes and expressions on the basis of the values provided to the case, case statement in Ruby is similar to basic switch cases in the other programming languages.


1 Answers

It's not valid ruby syntax.

What you need is

case   when ARGV.length == 0     abort "Error 1"   when ARGV.length > 2     abort "Error 2" end 

When you write case x, the important part you need to understand is that ruby takes the x and then applies a comparison to the argument or expressions you insert in the when clause.

The line where you say when x >2 reads to ruby like:

if ARGV.length == > 2 

When you remove a specific object from the case statements, you can apply conditionals within the when statements .

like image 121
Philip Reynolds Avatar answered Sep 29 '22 18:09

Philip Reynolds