Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9168589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:39:39+00:00 2026-06-17T15:39:39+00:00

Fyi, using Rails. Given user = User.find(1) This case statement returns nil when it

  • 0

Fyi, using Rails.

Given user = User.find(1)

This case statement returns nil when it should return the result of self.do_something_with_user.

def case_method
    case self.class
      when User
        self.do_something_with_user # assume does not return nil
      when SomeOtherClass
        self.do_something_else
      else
        nil
    end
end

user.case_method # => nil

What am I missing? Using pry, self.class == User returns true.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:39:40+00:00Added an answer on June 17, 2026 at 3:39 pm

    Ruby’s case statement is much more flexible than most other switch statements. It uses the === operator, not the == operator. Classes define the === operator along the lines of

    def ===(other)
    other.is_a? self #self is the class
    end

    So, what you actually want here is:

    def case_method
      case self
      when User
       do_something_with_user
      when SomeOtherClass
        do_something_else
      end # else is un-needed as it will return nil by default
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Solution Guys... FYI i am using xampp to use phpmyadmin. and this error happens
FYI, There is some overlap in the initial description of this question with a
I have built a blog application using Ruby on Rails. In the application I
I am trying to retrieve the exact address from the given lat/lng using the
Possible Duplicate: NOTICES for sequence after running migration in rails on postgresql Application Using
I have a very strange problem. (fyi - I'm using forms authentication and manually
I am using this jQuery plugin to lazy-load my images. Lazy Loader It is
Using rails and rspec it's easy to have rspec generate the necessary files for
I'm using codebrew\backbone-rails in a nested model example (say I have a collection of
I have a case where using a JOIN or an IN will give me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.