Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

RUBY: Range Converted to Array (Normal behavior?)

For general discussion related FlowStone

RUBY: Range Converted to Array (Normal behavior?)

Postby tiffy » 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.
Attachments
Range Converted to Array-Nomal Behaviour.fsm
(779 Bytes) Downloaded 841 times
User avatar
tiffy
 
Posts: 400
Joined: Wed May 08, 2013 12:14 pm

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

Postby KG_is_back » Sun Aug 13, 2017 9:08 pm

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
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Postby tulamide » Sun Aug 13, 2017 10:08 pm

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
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2688
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Postby tiffy » Sun Aug 13, 2017 10:33 pm

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:
User avatar
tiffy
 
Posts: 400
Joined: Wed May 08, 2013 12:14 pm


Return to General

Who is online

Users browsing this forum: No registered users and 40 guests