Generate Set from Array with the bounds

Viewed 69

I want to know is some sort of "Array.to_range" method in order to create some Range from an Array of two elements. I know that i could perform something like this:

a = [1,5]
Set.new(a[0]..a[1])
 => #<Set: {1, 2, 3, 4, 5}>

This a good solution, however i try to synthesize more, to archive something like this:

a = [1,5]
Set.new(a.to_range)

because if i do this, generate a Set with only 2 elements and i want the full range of elements.

a = [1,5]
Set.new(a)
 => #<Set: {1, 5}> 

and obviously

{1, 5} != {1, 2, 3, 4, 5}

So, any idea how can i synthesize this expression more?

Set.new(a[0]..a[1])
1 Answers

I think this give you what you are looking for:

a = [1,5]

Set.new(Range.new(*a)) # =>  #<Set: {1, 2, 3, 4, 5}>
Related