1

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?

4 Answers 4

5

check this

s_query.length.between?(5, 20)
Sign up to request clarification or add additional context in comments.

Comments

3

You almost got it:

 (5..20).cover? s_query.length

Comments

1

Also s_query.length.between?(5,20)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.