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
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
RUBY: Range Converted to Array (Normal behavior?)
4 posts
• Page 1 of 1
RUBY: Range Converted to Array (Normal behavior?)
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.
- Attachments
-
- Range Converted to Array-Nomal Behaviour.fsm
- (779 Bytes) Downloaded 860 times
-
tiffy - Posts: 400
- Joined: Wed May 08, 2013 12:14 pm
Re: RUBY: Range Converted to Array (Normal behavior?)
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:
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:
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?)
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.
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: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: RUBY: Range Converted to Array (Normal behavior?)
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.
What will I do without guys like you.
-
tiffy - Posts: 400
- Joined: Wed May 08, 2013 12:14 pm
4 posts
• Page 1 of 1
Who is online
Users browsing this forum: Google [Bot] and 56 guests