 # 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
elsif random_insert == 1
elsif random_insert == 2
elsif random_insert == 3
elsif random_insert == 4
end
``````

Is there a better way??

Thanks…

Dave

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.

Il giorno Wed, 8 Feb 2012 06:00:09 +0900
Dave C. [email protected] ha scritto:

``````      elsif random_insert == 1
``````

Thanks…

Dave

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”}

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”]

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

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

On Wed, Feb 08, 2012 at 06:00:09AM +0900, Dave C. wrote:

``````      elsif random_insert == 1
elsif random_insert == 2
elsif random_insert == 3
elsif random_insert == 4
end
``````

Is there a better way??

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}
``````

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 Tue, Feb 7, 2012 at 16:00, Dave C.
[email protected] wrote:

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

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 10:00 PM, Dave C.
[email protected] wrote:

``````elsif random_insert == 1
elsif random_insert == 2
elsif random_insert == 3
elsif random_insert == 4
end
``````

Is there a better way??

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

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

(random_insert + 10).to_s(16)

On Fri, Feb 17, 2012 at 9:54 AM, Ronnie Collinson
[email protected] 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)

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.