2

Why do I get "thumbs down"?

p = Post.new  

case p.class
when Post
  "Thumbs up"
else
  "Thumbs down"
end
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232

2 Answers2

6

You don't need to check classes specifically. Check instances, this is how case works.

p = Post.new  

case p
when Post
  "Thumbs up"
else
  "Thumbs down"
end

"I have this thing p, what might it be? Is it a Post or maybe a string that conforms to this regex? Or some another thing?"

More info on threequals operator, the power behind ruby's case expression: https://stackoverflow.com/a/4528453/125816

Community
  • 1
  • 1
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
0

"p.class" is the class, not the class name, try "p.class.name" instead.

Kulvar
  • 1,139
  • 9
  • 22