From: "D. Deryl Downey" Date: 2013-03-04T06:24:55+09:00 Subject: Re: Confusion with Ruby's "case/when" block statement As *clearly* stated (even within ANY ruby language tutorial explaining how ruby works) "A case expression searches for a match by starting at the first (top left) comparison, performing comparison === target. When a comparison returns true, the search stops, and the body associated with the comparison is executed. case then returns the value of the last expression executed. If no comparison matches: if an else clause is present, its body will be executed; otherwise, case silently returns nil." There's the 'test' it performs, AND what method is run *for* the test, AND what happens afterwards. READ IT! On 3/3/13 3:50 PM, "Kumar R." wrote: >Ruby uses `===` operator on the `case/when` type execution style.Now It >also known that Ruby depending on the type >of the thing present in the `when` clause, calls the respective `.===` >method. > >Say `when` statement contains the `class` names, then the rule is - ` it >will use Module#===, which will return true if the right side is an >instance of, >or subclass of, the left side.` One example with this context is: > >Here `instance of` test occurs > >obj = 'hello' >#=> "hello" >case obj >when String >print 'It is a string' >when Fixnum >print 'It is a number' >else >print 'It is not a string' >end >#It is a string >#=> nil > >Here `subclass of` test occurs > >num = 10 >#=> 10 >case num >when Numeric >puts "Right class" >else >puts "Wrong class" >end >#Right class >#=> nil > >Now `when` contains `String` literals then String#=== is called, which >in turn checks if left and right handside >literal are same(same chracter in same sequence) or not. > >a = "abc" >#=> "abc" >case a >when "def" then p "Hi" >when "abc" then p "found" >else "not found" >end >#"found" >#=> "found" > >The all logic is too cool. Now my query is with `case/when` structure - > >How does ruby know if `when` holding `class`, or `String` literals or >anything valid at runtime? > >or > >What test does it perform before calling the respective `.===` operator. > >-- >Posted via http://www.ruby-forum.com/. >