```On Fri, Jan 21, 2011 at 11:16 AM, Josh Rowe <joshua / wired.com.au> wrote:
> I now have it working, this is what it looks like
>
> card1 =3D {:name =3D> "Celtic guardian", :type =3D> "monster", :atk =3D> =
1400,
> :def =3D> 1200, :level =3D> 4}
> card2 =3D {:name =3D> "Dark Magician", :type =3D> "monster", :atk =3D> 25=
00,
> :def =3D> 2100, :level =3D> 7}
> card3 =3D {:name =3D> "Spike Seadra", :type =3D> "monster", :atk =3D> 160=
0, :def
> =3D> 1300, :level =3D> 5}
> \$deck =3D [card1,card2,card3]
> def draw
> =A0draw =3D rand(\$deck.size)
> =A0puts draw
> =A0puts "you drew the card #{\$deck[draw][:name]}"
> =A0\$deck.delete(\$deck[draw])

I'd rather use Array#delete_at because it is more efficient.  Your
solution needs to traverse the whole Array to find the elements to
remove.  Yes, in fact it may remove multiple elements!
Array#delete_at just removes a single position.

Btw, you can make the code even simpler and more efficient by doing:

def draw
draw =3D rand(\$deck.size)
puts draw
card =3D \$deck.delete_at(draw)
puts "you drew the card #{card[:name]}"
end

This avoids one Array access.

> end
> 3.times do
> =A0draw
> =A0x =3D \$deck.empty?
> =A0if x =3D=3D true
> =A0 =A0puts "Game over!"
> =A0end
> end

Comparing x with true is a bad idea because there are multiple values
for true in Ruby.  Generally comparing boolean values or expressions
with boolean constants to get a boolean value is a bad idea because

a) it is superfluous (we do have a boolean value already)
b) it leads to subtle errors which are hard to detect in all languages
which have more than one value representing either boolean state true
and false.

In your case you can simplify to

if \$deck.empty?
puts "Game over!"
end

or even

puts "Game over!" if \$deck.empty?

> Thank you both for answering my beginner questions.

You're welcome.

> I hope I will be
> able to help people on this forum one day.

Certainly!

Kind regards

robert

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

```