A better way?

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/.

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" <dcastellano1@wideopenwest.com> 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/\.

You can create a hash having the possible values of random_insert as keys and
the corresponding values you want to assign to correct_answer_shuffled as
values:

possible_answers = {0 => "A", 1 => "B", 2 => "C", 3 => "D", 4 => "E"}
correct_answer_shuffled = possible_answers[random_insert]

Given that the values of random_insert are positive integers, you can also
use an array instead of a hash:

possible_answers = ["A","B", "C", "D", "E"]
correct_answer_shuffled = possible_answers[random_insert]

You could also use a case expression, which gives code more similar to what
you wrote:

correct_answer_shuffled = case random_insert
  when 0 then "A"
  when 1 then "B"
  when 2 then "C"
  when 3 then "D"
  when 4 then "E"
end

This is best used when you have to perform different operations depending on
the value of random_insert, rather than using a different, fixed value. In
this case, an array or hash are better solution.

I hope this helps

Stefano

···

Il giorno Wed, 8 Feb 2012 06:00:09 +0900 Dave Castellano <dcastellano1@wideopenwest.com> ha scritto:

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

One option:

    correct_answer_shuffled = case random_insert
    when 0 then 'A'
    when 1 then 'B'
    when 2 then 'C'
    when 3 then 'D'
    when 4 then 'E'
    end

Another option:

    answers = %w{A B C D E}
    correct_answer_shuffled = answers[random_insert]

I could offer other suggestions as well, but they would involve making
assumptions about your code outside of the snippet you provided, and as
such might not solve the problem you're actually trying to solve as
demonstrated explicitly by your sample code.

···

On Wed, Feb 08, 2012 at 06:00:09AM +0900, 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??

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

If you want to pick a random item from a list Array#sample is the simplest:

irb(main):011:0> a = %w{A B C D E}
=> ["A", "B", "C", "D", "E"]
irb(main):012:0> a.sample
=> "E"
irb(main):013:0> a.sample
=> "D"
irb(main):014:0> a.sample
=> "E"
irb(main):015:0> a.sample
=> "D"

Kind regards

robert

···

On Tue, Feb 7, 2012 at 10:00 PM, Dave Castellano <dcastellano1@wideopenwest.com> 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 = &quot;A&quot;
     elsif random\_insert == 1
       correct\_answer\_shuffled = &quot;B&quot;
     elsif random\_insert == 2
       correct\_answer\_shuffled = &quot;C&quot;
     elsif random\_insert == 3
       correct\_answer\_shuffled = &quot;D&quot;
     elsif random\_insert == 4
       correct\_answer\_shuffled = &quot;E&quot;
     end

Is there a better way??

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Congratulations! In addition to what the others have said, to simply
solve the current problem, this attitude is a significant milestone in
becoming a *good* programmer!

-Dave

···

On Tue, Feb 7, 2012 at 16:00, Dave Castellano <dcastellano1@wideopenwest.com> wrote:

As I am learning to program, I am finding things I think can probably be
done in a better way.

--
Dave Aronson: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

If the letters dont go higher then F, you could add 10 and convert to
a hex (string)

(random_insert + 10).to_s(16)

There's another approach: using the alphabet:

irb(main):024:0> char_a = 'A'.bytes.first
=> 65
irb(main):025:0> (char_a + rand(26)).chr
=> "G"

or

random_insert = ...
(char_a + random_insert).chr

Cheers

robert

···

On Fri, Feb 17, 2012 at 9:54 AM, Ronnie Collinson <notthinking@gmail.com> wrote:

If the letters dont go higher then F, you could add 10 and convert to
a hex (string)

(random_insert + 10).to_s(16)

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/