RUBY: Range Converted to Array (Normal behavior?)
Posted: Sun Aug 13, 2017 4:02 pm
RUBY: Range Converted to Array
I included some examples that works and two that does not work.
Ruby allows the conversion of a Range (1..5) into an Array [1,2,3,4,5], when that Range is directly entered into the Ruby interpreter:
b = (1..5) # Directly entered into the Ruby interpreter
output 0, (b).to_a => [1,2,3,4,5] # Correctly converted.
However, if you do not enter the Range (1..5) directly into the Ruby interpreter but by means of the Ruby input terminal, then the conversion fails:
b = @b # Entered by means of Ruby input terminal, where b is still equal to (1..5).
output 0, (b).to_a => ["1..5"] # Incorrect conversion.
My question is:
1) Am I doing something wrong here in Ruby, or is this normal behavior that Ruby does not accept a Range entered like this (1..5) as an input variable by means of the Ruby interpreter's input terminal ?
2) Is the only means then to enter a Range like (Min..Max) as two separate input values (Min) and (Max) when using the Ruby interpreter input terminals?
See schematics included as examples.
I included some examples that works and two that does not work.
Ruby allows the conversion of a Range (1..5) into an Array [1,2,3,4,5], when that Range is directly entered into the Ruby interpreter:
b = (1..5) # Directly entered into the Ruby interpreter
output 0, (b).to_a => [1,2,3,4,5] # Correctly converted.
However, if you do not enter the Range (1..5) directly into the Ruby interpreter but by means of the Ruby input terminal, then the conversion fails:
b = @b # Entered by means of Ruby input terminal, where b is still equal to (1..5).
output 0, (b).to_a => ["1..5"] # Incorrect conversion.
My question is:
1) Am I doing something wrong here in Ruby, or is this normal behavior that Ruby does not accept a Range entered like this (1..5) as an input variable by means of the Ruby interpreter's input terminal ?
2) Is the only means then to enter a Range like (Min..Max) as two separate input values (Min) and (Max) when using the Ruby interpreter input terminals?
See schematics included as examples.