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)


On Thursday, July 31, 2014 8:45:33 AM UTC-6, Rob Biedenharn wrote:
>
>
> On 2014-Jul-30, at 10:59 , Eric Saupe <eric...@gmail.com <javascript:>> 
> 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 ]
> arrays.sample.push(id)
>
> http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-sample
>
> -Rob
>
>
> 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 <javascript:>.
> To post to this group, send email to rubyonra...@googlegroups.com 
> <javascript:>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/62d3b861-c606-4936-8540-08caae33e8fc%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/rubyonrails-talk/62d3b861-c606-4936-8540-08caae33e8fc%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
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-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/931a7b70-e8bf-464a-90d8-ae0f4682ee3a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to