Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What is the best way to use a class object in case statement? Suppose I have a which is an instance of the Class class. I want to match it against different classes. If I do

case a
when String then ...
when Fixnum then ...
end

this will not give the intended result because even if a == String for example, a === String is not true. What is the clever way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
580 views
Welcome To Ask or Share your Answers For Others

1 Answer

I wouldn't use to_s, because "String".to_s would be "String", so maybe I'd do

case
when a == String then ...
when a == Fixnum then ...
end

or

a = String

case [a]
when [String] then puts "String"
when [Array] then puts "Array"
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...