Ruby Control Structures - Case (Bonus Question)

Sort by

recency

|

82 Discussions

|

  • + 0 comments

    why is: *case obj * and not: case obj.class

  • + 1 comment

    If you're doing the same thing for each case, a case statement doesn't really make sense in my opinion.

    Here's a predicate that includes all of the conditions:

    [Hacker, Submission, TestCase, Contest].any? { |clazz| obj.is_a? clazz }
    

    You can use string interpolation (like in truesdell_trent1's comment here ) or just concatenation (with obj.class.name) to output the class names.

  • + 0 comments
    def identify_class(obj)
        # write your case control structure here
        case obj
        when Hacker
            puts "It's a #{obj.class}!"
        when Submission
            puts "It's a #{obj.class}!"
        when TestCase
            puts "It's a #{obj.class}!"
        when Contest
            puts "It's a #{obj.class}!"
        else
            puts "It's an unknown model"
        end 
    end
    
  • + 0 comments
    def identify_class(obj)
        # 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
            puts "It's an unknown model"
            
        end
    end
    
  • + 0 comments

    https://bestbeardstraightener.com/