Page 1 of 1

RUBY: Range Converted to Array (Normal behavior?)

Posted: Sun Aug 13, 2017 4:02 pm
by tiffy
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.

Re: RUBY: Range Converted to Array (Normal behavior?)

Posted: Sun Aug 13, 2017 9:08 pm
by KG_is_back
the problem is rather obvious - in the second example what you actually do is you input a string value "1..5" in a variable. When you then have the b.to_a what ruby does is it converts the string into most obvious array it can - array with single element being the string itself ["1..5"]
There are two ways you can fix this. The bad way is to let ruby interpret the string as code using eval() method. This can be done for example like this:

Code: Select all

#@b contains the string "1..5"
b=eval(@b)
b.to_a

What eval does it basically behaves as if "eval(@b)" got replaced by the value of @b in the code. You might see why this is a problem - it will execute any code given with no discrimination. If you're not careful you can break your code with invalid syntax, crash ruby/flowstone or (with a bit of cleverness) even format your hard-drives.

The good way to do it is to create Range object the traditional way:

Code: Select all

b=Range.new(start,end,exclusive) #start,end are integers, exclusive is boolean and false by default

Re: RUBY: Range Converted to Array (Normal behavior?)

Posted: Sun Aug 13, 2017 10:08 pm
by tulamide
Hey KG, good to see you around here :)

Tiffy,

in addition to KG's post (I like to add to never use eval unless you're a Ruby pro, whereby I consider myself far from being a pro :!: ) I think your basic misconception is that a range would just be two numbers with dots between them. A Range is an object (aka class) just as everything else in Ruby - and in Ruby alone! As soon as you pull something out of Ruby, it loses its meaning. In your case, to be able to feed a RubyEdit with a range, you would need a green Range prim, imitating Ruby's Range class. But you feed it with a string, which is a totally different object (aka class) than the Range class. If you would like to have an automatic conversion from a string class to a range class, you'd need to extend the string class with that functionality.

Code: Select all

class String
  def to_range
    #all your conversion code belongs here
  end
end

Re: RUBY: Range Converted to Array (Normal behavior?)

Posted: Sun Aug 13, 2017 10:33 pm
by tiffy
Thank you so much KG and tulamide. I appreciate your time and effort, both of you...I tried your examples and it works! Super, now I can use a single string input.

What will I do without guys like you. :mrgreen: