We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Ruby
  3. Control Structures
  4. Ruby Control Structures - Case (Bonus Question)
  5. Discussions

Ruby Control Structures - Case (Bonus Question)

  • Problem
  • Submissions
  • Leaderboard
  • Discussions

Sort 47 Discussions, By:

votes
  • recency
  • votes

Please Login in order to post a comment

  • ekanade 3 years ago+ 2 comments
    def identify_class(obj)
        case obj
        when Hacker,Submission,TestCase,Contest
            puts "It's a #{obj.class}!"
        else
            puts "It's an unknown model"
        end
    end
    

    this works incase someone is looking for different solutions.

    15|
    Permalink
    • ksam95 3 years ago+ 1 comment

      Can you explain?

      0|
      ParentPermalink
      • keithchongwy 2 years ago+ 1 comment

        he is using interpolation within the double quoted strings. Hence obj.class will be processed or evaluated first before it turns into a string. (respectively in order: "Hacker", "Submission", "TestCase", and then "Contest")

        as soon as he grouped the whens in the case, it is almost as if he is saying

        if obj == Hacker || obj == Submission || obj == TestCase || obj == Contest puts "It's a #{obj.class}!" else puts "It's an unknown model"

        Here is the unrefactored version

        write your case control structure here

        case obj
        when Hacker
            puts "It's a Hacker!" 
        when Submission
            puts "It's a Submission!"
        when TestCase
            puts "It's a TestCase!"
        when Contest
            puts "It's a Contest!"
        else
            "It\'s an unknown model"
        end
        

        end

        0|
        ParentPermalink
        • martindeto 5 months ago+ 0 comments

          I had exactly this and it failed the test. The refactored version of yours worked though. I am seriously trying to figure out why. Actually even this isn't working now, but the refactored one is working.

          0|
          ParentPermalink
    • dongxialiu2002 2 years ago+ 0 comments

      it won't pass the test case 1

      0|
      ParentPermalink
  • phillipr360 2 years ago+ 0 comments

    This problem statement is extremely misleading and needs to be rewritten.

    As it is stated, we are lead to believe that Hacker, Submission, TestCase, and Constants are all separate classes inheriting from the same superclass. This means that the following solution should work:

        case obj
        when Hacker, Submission, TestCase, Contest
            puts "It's a #{obj.class}!"
        else
            puts "It's an unknown model"
        end
    

    However, it fails test case 1. Moreover, some of the solutions listed below suggest that Hacker, Submission, TestCase, and Constant are really integer constants euqivalent to to 1, 2, 3, and 4.

    Obviously, using integer constants instead of subclasses goes against the problem definition (as currently written). While it will work for basic solutions checking each class type separately, more elaborate solutions taking advantage of the obj.class value will fail. Moreover, testing one solution using subclasses and another using integer constants is extremely misleading; if our solution is expected to work for both situations, the problem statement should explicitly say so.

    2|
    Permalink
  • machadolopes 4 years ago+ 1 comment

    def identify_class(obj)
    # write your case control structure here
    case obj
    when 1
    puts "It's a Hacker!"
    when 2
    puts "It's a submission!"
    when 3
    puts "It's a TestCase!"
    when 4
    puts "It's a Contest!"
    else
    puts "It's an unknown model"
    end
    end
    this code worked fine on IRB passing the input via method like so: identify_class(10), identify_class(3), etc But it's not working here.

    2|
    Permalink
    • jrcharney 4 years ago+ 0 comments

      I know right. The input for this example screws up everything!

      0|
      ParentPermalink
  • kishancs46 4 years ago+ 4 comments

    Why does this work?

    case obj
    
    when Hacker, Submission, TestCase, Contest 
    
        puts "It's a #{obj.class}!"
    else 
    
        puts"It's an unknown model"
    
    end
    

    When "k" == String it says false,

    "k" === String is also false

    what is the case logic in ruby?

    1|
    Permalink
    • dimitrynazarov 4 years ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • dimitrynazarov 4 years ago+ 4 comments

      Got the same solution and its failing on every test case. Can't figure out what's happening:

      def identify_class(obj)
        case obj
        when "Hacker", "Submission", "TestCase", "Contest"
          puts "It\'s a #{obj}!"
        else
          puts "It\'s an unknown model"
        end
      end
      

      gettting correct reuturns when running it locally:

      identify_class('Hacker')
      identify_class('Submission')
      identify_class('TestCase')
      identify_class('Submission')
      identify_class('Contest')
      
      2|
      ParentPermalink
      • dimitrynazarov 4 years ago+ 0 comments

        mmann, I fall in the same trap all the time. Jumping into writing code without reading and fully understanding the task first. We're asked to identify classes, not strings

        1|
        ParentPermalink
      • dipakpurbey 4 years ago+ 0 comments

        use directly the class name instead of it as string.. Hacker instead of "Hacker"

        1|
        ParentPermalink
      • sweetmannc999 3 years ago+ 0 comments

        try this

        def identify_class(obj) case obj.class.to_s when "Hacker", "Submission", "TestCase", "Contest" puts "It's a #{obj.class}!" else puts "It's an unknown model" end end

        2|
        ParentPermalink
      • jueluddin39 2 years ago+ 0 comments

        because "Hacker", "Submission", "TestCase", "Contest" are not strings.

        0|
        ParentPermalink
    • ahmadhasankhan 4 years ago+ 5 comments

      Try this.

      def identify_class(obj)
          # write your case control structure here
          case obj.class.to_s
          when "Hacker"
              puts "It's a Hacker!"
          when "Submission"
              puts "It's a Submission!"
          when "TestCase"
              puts "It's a TestCase!"
          when "Contest"
              puts "It's a Contest!"
          else 
              puts "It's an unknown model"
          end    
      end
      
      0|
      ParentPermalink
      • lyc950421 3 years ago+ 0 comments

        This works .. thx

        0|
        ParentPermalink
      • ChristianVelez 3 years ago+ 3 comments

        This answer certainly works but the idea of turning a class into a string could lead to potential issues down the line and made me a bit uncomfortable.

        This worked for me in case anyone is interested:

        def identify_class(obj)
            case obj
                when Hacker
                puts "It\'s a Hacker!"
        
                when Submission
                puts "It\'s a Submission!"
        
                when TestCase
                puts "It\'s a TestCase!"
        
                when Contest
                puts "It\'s a Contest!" 
                
                else
                puts "It\'s an unknown model"    
            end  
        end
        
        1|
        ParentPermalink
        • lynnshep89 3 years ago+ 0 comments

          I did this same thing, yet I am not passing both Test Cases.

          0|
          ParentPermalink
        • ahmadhasankhan 3 years ago+ 0 comments

          Hey @ChristianVelez, will you please elaborate how turning a class name into a string could lead to potential issues ?

          0|
          ParentPermalink
        • ahmadhasankhan 2 years ago+ 0 comments
          [deleted]
          0|
          ParentPermalink
      • mail_vivekr 2 years ago+ 1 comment

        obj.class.to_s means....what is to_s

        0|
        ParentPermalink
        • collinhurst12 2 years ago+ 0 comments

          to string

          0|
          ParentPermalink
      • forevertkj4 2 years ago+ 0 comments

        can you explain, why that code its work ?

        0|
        ParentPermalink
      • danielgcwik 1 year ago+ 0 comments

        it worked for me thanks

        0|
        ParentPermalink
    • bazzargh 4 years ago+ 0 comments

      case comparisons are done using ===, so this is the same as if Hacker === obj ... (and so on). The === method of Class is inherited from Module, and is documented as:

      "Returns true if obj is an instance of module or one of module 's descendants. Of limited use for modules, but can be used in case statements to classify objects by class."

      So it's also asymmetric - "k" === String is false, but String === "k" is true.

      6|
      ParentPermalink
  • saurabh03 4 years ago+ 2 comments

    I tried solving this by NOT using the case statement. I did something like this

    def identify_class(obj)

    if !obj.nil? && ['Hacker', 'Submission', 'TestCase', 'Contest'].include?(obj.class.to_s) puts("It\'s a #{obj.class}!") else puts("It\'s an unknown mode!") end
    end

    This works for general test case but fails for the hidden one. Any pointers what is causing this failure?

    1|
    Permalink
    • saurabh03 4 years ago+ 0 comments

      Please ignore it, fault was the typo at the end. I should have written 'model' instead of 'mode!'

      0|
      ParentPermalink
    • MoonWalkerPTAsked to answer 4 years ago+ 1 comment

      Jesus!... you could just use a case statement. It's easier, a lot!

      0|
      ParentPermalink
      • saurabh03 4 years ago+ 0 comments

        Yes no particular reason for not using the recommended 'case' statement.

        0|
        ParentPermalink
  • pasha_zhukov 4 days ago+ 0 comments

    It could be abused by puts "It's a #{obj.class.name}!" I suppose tests or task should be improved :)

    0|
    Permalink
  • kurumbeths 1 month ago+ 0 comments

    i tried this way not working

    def identify_class(obj) case obj when Hacker,Submission,TestCase,Contest puts "it's #{obj.class}!" else puts "it's an unknown model" end

    # write your case control structure here
    

    end

    0|
    Permalink
  • krishnasinghcs 5 months ago+ 0 comments

    Hello Hackerrank,

    On my system I am getting correct result with

    [Hacker, Submission, TestCase, Contest].include?(obj.class) ? "It's a #{obj.class}!" : "It's an unknown model"
    

    But while submitting it is giving no out put

    0|
    Permalink
  • p0lym4th 7 months ago+ 0 comments

    Why isn't it case obj.class ?

    0|
    Permalink
  • artur_ludwik 10 months ago+ 0 comments
    def identify_class(obj)
        case obj
            when Hacker 
                puts "It's a Hacker!"
            when Submission 
                puts "It's a Submission!"
            when TestCase 
                puts "It's a TestCase!"
            when Contest
                puts "It's a Contest!"
            else puts "It's an unknown model"
        end
    end
    
    0|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature