case (variable) when type1 ... when type2 ... end

E

Eugen Ciur

Hi all,
how can I write case statement to check of specific type the variable has?
Something like:

case var
when :Fixnum
puts "#{var} is Fixnum"
when :String
puts "#{var} is String"
end

I tried this also

case var.class
when 'Fixnum'
puts "#{var} is Fixnum"
when 'String'
puts "#{var} is String"
end

but ruby refuses to understand me :(
What is the best way to write conditional that depends of variable type?


Thank you!
 
J

Jean-Julien Fleck

how can I write case statement to check of specific type the variable has=
?
Something like:

It simpler:
case var
when :Fixnum
=A0puts "#{var} is Fixnum"
when :String
=A0puts "#{var} is String"
end

case var
when Fixnum
puts "#{var} is Fixnum"
when String
puts "#{var} is String"
end

(no ":")

Cheers,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

Eugen,


Monday, August 30, 2010, 12:39:36 AM, you wrote:

EC> Hi all,
EC> how can I write case statement to check of specific type the variable has?
EC> Something like:

EC> case var
EC> when :Fixnum
EC> puts "#{var} is Fixnum"
EC> when :String
EC> puts "#{var} is String"
EC> end

EC> I tried this also

EC> case var.class
EC> when 'Fixnum'
EC> puts "#{var} is Fixnum"
EC> when 'String'
EC> puts "#{var} is String"
EC> end

EC> but ruby refuses to understand me :(
EC> What is the best way to write conditional that depends of variable type?


EC> Thank you!

You were very close.

case var
when Fixnum
puts "#{var} is Fixnum"
when String
puts "#{var} is String"
end

Ralph
 
R

Robert Klemme

Hi, could you explain me why this works? It's a special behaviour of the
"case" statment?

"case" uses operator "=3D=3D=3D" to check conditions. If you do "case x
when y" then "y =3D=3D=3D x" is invoked to check the condition (note the
order of x and y). You can even cook your own

positive =3D lambda {|x| x >=3D 0}
class << positive
alias :=3D=3D=3D :call
end

(-4..4).each do |i|
case i
when positive
puts "#{i} pos"
else
puts "#{i} neg"
end
end

btw, there is also another form of case

case # no value here!
when i >=3D 0
puts "#{i} pos"
else
puts "#{i} neg"
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Klemme

Also (at least in 1.9.2) the alias is unnecessary, if a proc is provided
within a "when" statment, the var is automatically passed as proc's
argument ;)

Oh, that's great! IIRC I suggested this a while ago.

Thanks for the update! I haven't played with 1.9.2 yet.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top