Ruby Control Structures - Case (Bonus Question)

Sort by

recency

|

84 Discussions

|

  • + 0 comments

    Promotional products serve as powerful tools to strengthen brand visibility—just like Ruby control structures streamline decision-making in code. In the bonus case question, choosing the right case statement ensures clean, efficient logic, much like selecting the right branded item ensures lasting customer impact. Whether you're coding with precision or marketing with creativity, the right structure—be it in logic or promotion—leads to better outcomes and a more memorable user experience.

  • + 0 comments

    Here is Ruby Control Structures - Case (Bonus Question) solution - https://programmingoneonone.com/hackerrank-ruby-control-structures-case-bonus-question-problem-solution.html

  • + 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