bidgeeboy
bidgeeboy

Reputation: 59

erb defining a min and maximum

I need to apply a conditional for a query only for results between two numbers (1 and 9).

I can't find a way of combining the greater than and less than operators and all I have the below which doesn't work.

Any help appreciated.

<% if @groups[location].size [1..9] %>

Upvotes: 1

Views: 168

Answers (2)

mrzasa
mrzasa

Reputation: 23327

You can use Range#include?

[2] pry(main)> (1..9).include? 2
=> true

And in your case

<% if (1..9).include?(@groups[location].size) %>

I'd prefer two conditions probably:

<% if @groups[location].size >= 1 && @groups[location].size <= 9 %>

EDIT:

Just as Mr. Sergio predicted I'd prefer Comparable#between?:

<% @groups[location].size.between?(1,9) %>

Range#cover? is quite good as well:

<% if (1..9).cover?(@groups[location].size) %>

Worth reading: What is the difference between `Range#include?` and `Range#cover?`?

Upvotes: 3

steenslag
steenslag

Reputation: 80075

"(...) between two numbers (1 and 9)."

<% if @groups[location].size.between?(1, 9) %>

Upvotes: 5

Related Questions