Variable + string to specify variable

16 views
Skip to first unread message

Dave Castellano

unread,
Jul 29, 2014, 9:34:46 PM7/29/14
to rubyonra...@googlegroups.com
Hi,

Novice question:

I need to assign an item id to one of 3 arrays randomly but can't figure
out how to specify the correct array by combining "arr" and the randomly
generated number...

id = "100"
x = rand(1..3)

arr1 = Array.new
arr2 = Array.new
arr3 = Array.new

selected_array = "#{'arr' + x}" *** Not sure how to do this ****

selected_array.push(id)

Thanks in advance,

Dave Castellano

--
Posted via http://www.ruby-forum.com/.

Scott Ribe

unread,
Jul 29, 2014, 10:05:16 PM7/29/14
to rubyonra...@googlegroups.com
On Jul 29, 2014, at 7:33 PM, Dave Castellano <li...@ruby-forum.com> wrote:

> Novice question:
>
> I need to assign an item id to one of 3 arrays randomly but can't figure
> out how to specify the correct array by combining "arr" and the randomly
> generated number...
>
> id = "100"
> x = rand(1..3)
>
> arr1 = Array.new
> arr2 = Array.new
> arr3 = Array.new
>
> selected_array = "#{'arr' + x}" *** Not sure how to do this ****
>
> selected_array.push(id)
>

While you could append the name and eval, that's not really a clean approach. Just use an array of arrays. Or if else...


--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Eric Saupe

unread,
Jul 30, 2014, 10:59:36 AM7/30/14
to rubyonra...@googlegroups.com
To expand on what Scott is saying here is some code that gives an example of what he is referring to.

id = 100
x  
= rand(1..3)

arrays
= [Array.new, Array.new, Array.new]
selected_array
= arrays[x]
selected_array
.push(id)

Rob Biedenharn

unread,
Jul 31, 2014, 10:45:33 AM7/31/14
to rubyonra...@googlegroups.com
On 2014-Jul-30, at 10:59 , Eric Saupe <eric...@gmail.com> wrote:

To expand on what Scott is saying here is some code that gives an example of what he is referring to.

id = 100
x  = rand(1..3)
arrays = [Array.new, Array.new, Array.new]
selected_array = arrays[x]
selected_array
.push(id)

And to make it a bit more flexible, you could do:

arrays = [ Array.new,  Array.new,  Array.new ]
x = rand(arrays.size)
arrays[x].push(id)

or even:

arrays = [ Array.new,  Array.new,  Array.new ]

On Tuesday, July 29, 2014 8:05:16 PM UTC-6, Scott Ribe wrote:
On Jul 29, 2014, at 7:33 PM, Dave Castellano <li...@ruby-forum.com> wrote:

> Novice question:
>
> I need to assign an item id to one of 3 arrays randomly but can't figure
> out how to specify the correct array by combining "arr" and the randomly
> generated number...
>
> id = "100"
> x = rand(1..3)
>
> arr1 = Array.new
> arr2 = Array.new
> arr3 = Array.new
>
> selected_array =  "#{'arr' + x}"  *** Not sure how to do this ****
>
> selected_array.push(id)
>

While you could append the name and eval, that's not really a clean approach. Just use an array of arrays. Or if else...


--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice





--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/62d3b861-c606-4936-8540-08caae33e8fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Saupe

unread,
Jul 31, 2014, 12:00:47 PM7/31/14
to rubyonra...@googlegroups.com
I knew there would be a nice simpler Ruby way. I love the second solution, Rob. Below is the updated example.
id = 100

arrays = [Array.new, Array.new, Array.new]
arrays.sample.push(id)

Scott Ribe

unread,
Jul 31, 2014, 12:23:45 PM7/31/14
to rubyonra...@googlegroups.com, Eric Saupe
On Jul 31, 2014, at 10:00 AM, Eric Saupe <eric...@gmail.com> wrote:

> I knew there would be a nice simpler Ruby way. I love the second solution, Rob. Below is the updated example.

One tiny note, this is purely a matter of style & taste, spelling it out vs concision, but I thought it might be good for your learning to understand that you could also use:

arrays = [[], [], []]

And, if you wanted a set of arrays that you access by string instead of int:

arrays = {'one' => [], 'two' => [], 'three' => []}
arrays['one'].push(42)
puts arrays['one'][0]

Rob Biedenharn

unread,
Jul 31, 2014, 5:46:29 PM7/31/14
to rubyonra...@googlegroups.com
You can simplify the arrays construction, too: (with slight reformatting of the arrays output)

irb2.1.1> arrays = Array.new(3) { Array.new }
#2.1.1 => [[], [], []]
irb2.1.1> 100.times {|id| arrays.sample.push(id) }
#2.1.1 => 100
irb2.1.1> arrays
#2.1.1 => [[0, 2, 6, 8, 9, 11, 14, 16, 17, 18, 22, 
            24, 29, 31, 37, 42, 47, 49, 53, 54, 57, 
            58, 60, 68, 69, 73, 75, 79, 80, 81, 83, 
            88, 91, 94, 95, 96], 
           [1, 3, 12, 13, 20, 25, 26, 28, 33, 34,
            38, 39, 43, 44, 48, 50, 52, 59, 63, 65,
            70, 72, 74, 77, 78, 82, 86, 87, 89, 97],
           [4, 5, 7, 10, 15, 19, 21, 23, 27, 30, 
            32, 35, 36, 40, 41, 45, 46, 51, 55, 56,
            61, 62, 64, 66, 67, 71, 76, 84, 85, 90,
            92, 93, 98, 99]]
irb2.1.1> arrays.map(&:size)
#2.1.1 => [36, 30, 34]

Reply all
Reply to author
Forward
0 new messages