From: Ian Asaff Date: 2012-02-08T06:12:57+09:00 Subject: Re: A better way? Try an array: 1.9.3-p0 :008 > random_insert = 3 => 3 1.9.3-p0 :009 > correct_answer_shuffled = ("A".."E").to_a[random_insert] => "D" ("A".."E").to_a will create an array: ["A","B","C","D","E"]. Then, you use random_insert to grab the value at the appropriate index. On 2/7/12 4:00 PM, "Dave Castellano" wrote: >Hi, > >As I am learning to program, I am finding things I think can probably be >done in a better way. The following code seems like something >programmers must run into all the time and I am wondering if there is a >better way to write the code in this situation. I'm having fun and >trying to get better at this.... > > if random_insert == 0 > correct_answer_shuffled = "A" > elsif random_insert == 1 > correct_answer_shuffled = "B" > elsif random_insert == 2 > correct_answer_shuffled = "C" > elsif random_insert == 3 > correct_answer_shuffled = "D" > elsif random_insert == 4 > correct_answer_shuffled = "E" > end > >Is there a better way?? > >Thanks... > >Dave > >-- >Posted via http://www.ruby-forum.com/. >