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

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 question - creating random size arrays ad hok

For general discussion related FlowStone

Ruby question - creating random size arrays ad hok

Postby kortezzzz » Fri May 03, 2019 7:05 pm

Hi,

There is a feature that I wondered if Ruby can do.
What I'm looking for is a module that can output a random size array that based on fixed given values. For example:

If I feed to first input the value 34 and to the second input the value 4, the output then would be

34
34
34
34

Any chance that it's doable?
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby question - creating random size arrays ad hok

Postby tulamide » Fri May 03, 2019 7:34 pm

Code: Select all
output(0, Array.new(@size, @object))
sends to the first output, size and object being inputs, where size needs to be a number. Note, within Ruby it is @size times the SAME object)

use the event method if you only want to output, when a specific input is triggered.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby question - creating random size arrays ad hok

Postby trogluddite » Fri May 03, 2019 7:36 pm

Oh yes, Ruby makes that nice and easy.

You don't have to create an Array using the brackets notation - they're objects, just like everything else, so have a 'new' method, and it's surprisingly flexible...

Code: Select all
# A new, empty Array.
my_array = Array.new

# A new Array of the given size, with all elements set to nil.
my_array = Array.new(size)

# A new Array of the given size, with all elements set to 'value'.
my_array = Array.new(size, value)

# A new Array of the given size, with each element calculated separately in a code block.
my_array = Array.new(size) do |index|
  # Compute a value for the element, using the element's 'index' if necessary.
end
### OR ###
my_array = Array.new(size){|index| #Calculate }


There's one little caveat that you have to watch out for, depending what you want inside the Array. The "Array.new(size, value)" form doesn't make a new copy of the value for each element, they're all references to the original object. That's no problem for simple, numeric elements, but can catch you out if you're using, say, Strings or nested sub-Arrays...

Code: Select all
# All elements point to the same String...
my_array = Array.new(10, "hello")
my_array[3].upcase!   # Alters the String "in-place" without creating a new one.
my_array[3] #=> "HELLO"
my_array[9] #=> "HELLO"  -- oops, probably not what we wanted!

# Unique, but identical elements...
my_array = Array.new(10){|index| "hello" }
my_array[3].upcase!
my_array[3] #=> "HELLO"
my_array[9] #=> "hello" -- that's better!


@tulamide - you beat me to it! :D But I thought the "same object" thing was worth going into further, as it's such a common "gotcha" for new Rubyists when using container classes.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby question - creating random size arrays ad hok

Postby tulamide » Fri May 03, 2019 7:41 pm

@trog
That's totally fine! I was just too lazy to go into details :mrgreen:
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby question - creating random size arrays ad hok

Postby kortezzzz » Sat May 04, 2019 12:06 am

Thanks guys :D Works great
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm


Return to General

Who is online

Users browsing this forum: No registered users and 38 guests