Ruby Control Structures - Case (Bonus Question)
Ruby Control Structures - Case (Bonus Question)
ekanade + 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.
ksam95 + 1 comment Can you explain?
keithchongwy + 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
martindeto + 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.
dongxialiu2002 + 0 comments it won't pass the test case 1
phillipr360 + 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.
machadolopes + 1 comment def identify_class(obj)
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.
# 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
jrcharney + 0 comments I know right. The input for this example screws up everything!
kishancs46 + 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?
dimitrynazarov + 0 comments [deleted]dimitrynazarov + 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')
dimitrynazarov + 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
, notstrings
dipakpurbey + 0 comments use directly the class name instead of it as string.. Hacker instead of "Hacker"
sweetmannc999 + 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
jueluddin39 + 0 comments because "Hacker", "Submission", "TestCase", "Contest" are not strings.
ahmadhasankhan + 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
lyc950421 + 0 comments This works .. thx
ChristianVelez + 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
lynnshep89 + 0 comments I did this same thing, yet I am not passing both Test Cases.
ahmadhasankhan + 0 comments Hey @ChristianVelez, will you please elaborate how turning a class name into a string could lead to potential issues ?
ahmadhasankhan + 0 comments [deleted]
mail_vivekr + 1 comment obj.class.to_s means....what is to_s
collinhurst12 + 0 comments to string
forevertkj4 + 0 comments can you explain, why that code its work ?
danielgcwik + 0 comments it worked for me thanks
bazzargh + 0 comments case comparisons are done using
===
, so this is the same asif Hacker === obj ...
(and so on). The===
method ofClass
is inherited fromModule
, 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, butString === "k"
is true.
saurabh03 + 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
endThis works for general test case but fails for the hidden one. Any pointers what is causing this failure?
saurabh03 + 0 comments Please ignore it, fault was the typo at the end. I should have written 'model' instead of 'mode!'
MoonWalkerPTAsked to answer + 1 comment Jesus!... you could just use a case statement. It's easier, a lot!
saurabh03 + 0 comments Yes no particular reason for not using the recommended 'case' statement.
pasha_zhukov + 0 comments It could be abused by
puts "It's a #{obj.class.name}!"
I suppose tests or task should be improved :)
kurumbeths + 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
krishnasinghcs + 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
p0lym4th + 0 comments Why isn't it
case obj.class
?
artur_ludwik + 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
Sort 47 Discussions, By:
Please Login in order to post a comment