How can i check in ruby length of string, it's range, something like:
s_query.length?[5..20]
I'm new to ruby, how to code such code, for if check?
Here's something to meditate on:
(5 .. 10) === 'hello world'.length # => false (5 .. 10) === 'foo bar'.length # => true
This works because === is defined in Range to return a Boolean true/false whether the right side is within the left-side range.
From the documentation:
rng === obj → true or false click to toggle source
Returns true if obj is an element of the range, false otherwise. Conveniently, === is the comparison operator used by case statements.
case 79 when 1..50 then print "low\n" when 51..75 then print "medium\n" when 76..100 then print "high\n" end
produces:
high