[#285488] Zed Shaw - Ruby has dodged a bullet — Chuck Remes <cremes.devlist@...>

Much like watching a car accident in slow motion, I could scarcely

96 messages 2008/01/01

[#285678] Windows Compilation Madness — Gary Wright <gwtmp01@...>

No point in keeping this discussion in the Zed thread...

24 messages 2008/01/02

[#285742] change the symbol in an interpolated string value? — scooterm@...

PROBLEM:

11 messages 2008/01/03

[#285784] Ruby install on Kubuntu Linux - why so spread out — Tom Cloyd <tc@...>

I've migrated from WinXP in recent days, and I'm having trouble getting

19 messages 2008/01/03
[#285786] Re: Ruby install on Kubuntu Linux - why so spread out — Casimir <pikEISPAMMMseli@...> 2008/01/03

Tom Cloyd kirjoitti:

[#285792] Re: Ruby install on Kubuntu Linux - why so spread out — Peter Hickman <peter@...> 2008/01/03

Casimir I'm going to have to call you on this. As you may have just

[#285838] "wrong number of arguments" What? I must be thick or somethi — ole __ <oliver.saunders@...>

class PermutationIterator

10 messages 2008/01/03

[#285873] Time.gm(1969) chokes on Windows — Tim Ferrell <s0nspark@...>

18 messages 2008/01/03

[#285910] Get your hands dirty: Help bootstrap Ruby on Windows. — Luis Lavena <luislavena@...>

This was asked, and asked, and asked too many times, so here we go.

20 messages 2008/01/03

[#285951] Finding if an array contains a data type — Sam Phoneix <dominicjefferies@...>

Say I wanted to see if an array contained a number or not. Would I use

11 messages 2008/01/04

[#285981] How to put a Ruby website online without rails — Softmind Technology <softmindtechnology@...>

Hi,

28 messages 2008/01/04
[#286230] Re: How to put a Ruby website online without rails — James Britt <james.britt@...> 2008/01/05

Giles Bowkett wrote:

[#286252] Re: How to put a Ruby website online without rails — yudi <yudi.xue@...> 2008/01/05

Ramaze looks good, gotta take a look. Thanks :-)

[#286449] Re: How to put a Ruby website online without rails — James Dinkel <jdinkel@...> 2008/01/07

Here is a forum post that tells you how to embed ruby script into

[#285988] problem with sockets — ian@...

Hi

13 messages 2008/01/04

[#286012] Studying Blackjack (#151) — Ruby Quiz <james@...>

The three rules of Ruby Quiz:

42 messages 2008/01/04

[#286056] Re: zed shaw zed shaw zed shaw — "Rick DeNatale" <rick.denatale@...>

On Jan 4, 2008 11:52 AM, Jeremy McAnally <jeremymcanally@gmail.com> wrote:

16 messages 2008/01/04

[#286077] DBI doesn't seem to install correctly on CentOS — Xeno Campanoli <xcampanoli@...>

I tried installing with the old tarfile sequence using setup.rb, as I

14 messages 2008/01/04
[#286079] Re: DBI doesn't seem to install correctly on CentOS — Joshua Schairbaum <joshua.schairbaum@...> 2008/01/04

Try this to use DBI with MySQL:

[#286120] Best Ruby book for experienced programmer — Kamil Chmielewski <kamilski81@...>

Hello,

19 messages 2008/01/04

[#286221] Vintage 0.0.1 - The super slim, micro web framework based on the idea of the old Merb — "Jeremy McAnally" <jeremymcanally@...>

Vintage is a very small web framework based on the original idea of

23 messages 2008/01/05
[#286346] Re: [ANN] Vintage 0.0.1 - The super slim, micro web framework based on the idea of the old Merb — "ara.t.howard" <ara.t.howard@...> 2008/01/06

[#286273] Marshal Pipe — "Carlos J. Hernandez" <carlosjhr64@...>

I've just re-discovered pipes.

16 messages 2008/01/05
[#286515] Re: Marshal Pipe — Eric Hodel <drbrain@...7.net> 2008/01/07

On Jan 5, 2008, at 15:37 PM, Carlos J. Hernandez wrote:

[#286374] DateTime new_offset unexpected results — Greg Go <plant@...>

Hello, everybody:

13 messages 2008/01/06
[#286484] Re: DateTime new_offset unexpected results — "Todd Benson" <caduceass@...> 2008/01/07

On Jan 6, 2008 2:04 PM, Greg Go <plant@ultraspace.com> wrote:

[#286375] Ruby's Kernel::exec (and system and %x) — JJ <jjnoakes@...>

I was reading about Kernel::exec (and the related Kernel::system

29 messages 2008/01/06

[#286473] Where do I put the ".to_f" ? — Peter Bailey <pbailey@...>

Hello,

13 messages 2008/01/07

[#286493] Bacon 0.9, a small RSpec clone — Christian Neukirchen <chneukirchen@...>

Hello,

13 messages 2008/01/07

[#286508] Embedding 1.9 — Dave Thomas <dave@...>

Folks:

18 messages 2008/01/07

[#286558] Thin 0.5.1 LOLCAT released — Marc-andre Marc <macournoyer@...>

Hey all,

13 messages 2008/01/08

[#286656] JRuby 1.1 RC 1 Released — Thomas Enebo <Thomas.Enebo@...>

The JRuby community is pleased to announce the release of JRuby 1.1 RC 1

19 messages 2008/01/08
[#286710] Re: [ANN] JRuby 1.1 RC 1 Released — "Sander Land" <sander.land@...> 2008/01/08

With all the blog posts about JRuby being posted, I thought I would

[#286696] dow ruby's strftime not attempt POSIX-compliance? — Jochen Hayek <jochen+ruby-forum@...>

Why is ruby's core class Time acting like this:

21 messages 2008/01/08
[#286701] Re: dow ruby's strftime not attempt POSIX-compliance? — Suraj Kurapati <snk@...> 2008/01/08

Jochen Hayek wrote:

[#286702] Re: dow ruby's strftime not attempt POSIX-compliance? — Suraj Kurapati <snk@...> 2008/01/08

Suraj Kurapati wrote:

[#286717] Re: does ruby's strftime not attempt POSIX-compliance? — Jochen Hayek <jochen+ruby-forum@...> 2008/01/09

Suraj Kurapati wrote:

[#287161] Re: does ruby's strftime not attempt POSIX-compliance? — Suraj Kurapati <snk@...> 2008/01/12

Jochen Hayek wrote:

[#287217] Re: does ruby's strftime not attempt POSIX-compliance? — Jochen Hayek <jochen+ruby-forum@...> 2008/01/12

Pls let me assure you in the beginning of this note,

[#287219] Re: does ruby's strftime not attempt POSIX-compliance? — Suraj Kurapati <snk@...> 2008/01/12

Jochen Hayek wrote:

[#287344] Re: does ruby's strftime not attempt POSIX-compliance? — Yukihiro Matsumoto <matz@...> 2008/01/14

Hi,

[#286742] why does this code leak? — ara howard <ara.t.howard@...>

cfp2:~ > cat a.rb

35 messages 2008/01/09
[#286991] Re: why does this code leak? — "=?ISO-8859-2?Q?Rados=B3aw_Bu=B3at?=" <radek.bulat@...> 2008/01/10

T24gSmFuIDEwLCAyMDA4IDEwOjE1IFBNLCBSb2JlcnQgRG9iZXIgPHJvYmVydC5kb2JlckBnbWFp

[#287063] Here Document syntax is stringent - trailing blank — Todd Burch <promos@...>

text = <<EOD

12 messages 2008/01/11

[#287067] Need command line to run a file 4 times — jackster the jackle <contact@...>

Hi Ruby Forum...

13 messages 2008/01/11
[#287074] Re: Need command line to run a file 4 times — Xavier Noria <fxn@...> 2008/01/11

On Jan 11, 2008, at 2:47 PM, jackster the jackle wrote:

[#287080] Counting the files in a directory.... — "Kyle Schmitt" <kyleaschmitt@...>

SSdtIHdyaXRpbmcgc29tZSBzY3JpcHRzIHRvIGhlbHAgbWFuYWdlIGEgbWFpbCBzY2FubmVyIHVz

18 messages 2008/01/11

[#287095] IRB Keyboard Input Issues — Oliver Saunders <oliver.saunders@...>

When I press the up arrow in IRB to access history I see this:

13 messages 2008/01/11

[#287188] Stream Parsing with REXML — Marc Hoeppner <marc.hoeppner@...>

Hi (again, sort of) :)

13 messages 2008/01/12

[#287190] Callable class with block — blondinet <jblondinet@...>

Hi everyone,

12 messages 2008/01/12

[#287199] Re: gem build documentation for new gems? — Eric Hodel <drbrain@...7.net>

On Jan 12, 2008, at 07:57 AM, Giles Bowkett wrote:

11 messages 2008/01/12

[#287226] Ruby from source on Leopard excruciatingly slow internet talk — "Chris Shea" <chris@...>

Just a couple of days ago I "upgraded" from Tiger to Leopard, and the

19 messages 2008/01/13

[#287282] regualr expression (need help) — Heinrich Piard <linux@...>

Hi all,

15 messages 2008/01/13

[#287368] regular expression — Johnathan Smith <stu_09@...>

hey

15 messages 2008/01/14
[#287371] Re: regular expression — "Robert Klemme" <shortcutter@...> 2008/01/14

2008/1/14, Johnathan Smith <stu_09@hotmail.com>:

[#287414] any tricks to speed up ruby? — Roger Pack <rogerpack2005@...>

31 messages 2008/01/14
[#287423] Re: any tricks to speed up ruby? — James Britt <james.britt@...> 2008/01/14

Roger Pack wrote:

[#287583] Re: any tricks to speed up ruby? — "M. Edward (Ed) Borasky" <znmeb@...> 2008/01/16

James Britt wrote:

[#287525] Overridding A Method Via A Mixin — Andrew Stewart <boss@...>

Hi Everyone,

17 messages 2008/01/15
[#287526] Re: Overridding A Method Via A Mixin — Robert Klemme <shortcutter@...> 2008/01/15

On 15.01.2008 18:32, Andrew Stewart wrote:

[#287666] ruby annoyances ... mine is line continuation, what's yours? — "scooterm@..." <scooterm@...>

Just thinking how really smart Ruby is in most areas makes it all the

11 messages 2008/01/16

[#287720] FXRuby or Shoes? — Doug Livesey <biot023@...>

Sorry I've posted this in the generic Ruby thread, but I wanted as

14 messages 2008/01/17

[#287735] LDAP Server not connected error — Varun Goel <varun.rajeshkumar@...>

hi all i made authentication function like this

12 messages 2008/01/17
[#287752] Re: LDAP Server not connected error — "Matt Todd" <chiology@...> 2008/01/17

Pluck out the actual LDAP code into IRB and see if it works. I've not

[#287757] Ruby syntax in "respond_to do |format| line -- can clarify? — Joshua Beall <jbeall.ruby@...>

Hi All,

12 messages 2008/01/17

[#287819] passing method references in python & ruby — "rpardee@..." <rpardee@...>

Hey all,

21 messages 2008/01/18
[#287897] Re: passing method references in python & ruby — "rpardee@..." <rpardee@...> 2008/01/18

On Jan 17, 8:12 pm, Justin Collins <justincoll...@ucla.edu> wrote:

[#287914] Re: passing method references in python & ruby — "=?ISO-8859-2?Q?Rados=B3aw_Bu=B3at?=" <radek.bulat@...> 2008/01/18

SW4gcHl0aG9uIHdoZW4geW91IHVzZSBtZXRob2QgbmFtZSB3aXRob3V0IHBhcmVudGhlc2lzZXMg

[#287917] Re: passing method references in python & ruby — Justin Collins <justincollins@...> 2008/01/18

Rados梶w Buウat wrote:

[#287989] Re: passing method references in python & ruby — Robert Klemme <shortcutter@...> 2008/01/19

On 18.01.2008 20:11, Justin Collins wrote:

[#288034] Re: passing method references in python & ruby — Justin Collins <justincollins@...> 2008/01/19

Robert Klemme wrote:

[#287869] Longest Repeated Substring (#153) — Ruby Quiz <james@...>

The three rules of Ruby Quiz:

62 messages 2008/01/18
[#287931] Re: Longest Repeated Substring (#153) — yermej <yermej@...> 2008/01/18

On Jan 18, 2:38 pm, Ken Bloom <kbl...@gmail.com> wrote:

[#287932] Re: Longest Repeated Substring (#153) — "=?ISO-8859-2?Q?Rados=B3aw_Bu=B3at?=" <radek.bulat@...> 2008/01/18

T24gSmFuIDE4LCAyMDA4IDEwOjAwIFBNLCB5ZXJtZWogPHllcm1lakBnbWFpbC5jb20+IHdyb3Rl

[#287895] Thin 0.5.3 Purple Yogurt release — Marc-AndrCournoyer <macournoyer@...>

Hey all,

15 messages 2008/01/18
[#287962] Re: [ANN] Thin 0.5.3 Purple Yogurt release — "s.ross" <cwdinfo@...> 2008/01/19

This has been working so well on my Mac dev machine, I built it out on =20=

[#287973] Re: [ANN] Thin 0.5.3 Purple Yogurt release — Bob Hutchison <hutch@...> 2008/01/19

[#287984] Re: [ANN] Thin 0.5.3 Purple Yogurt release — "s.ross" <cwdinfo@...> 2008/01/19

Got it up and running ... for a while. It's running as root/root and

[#287999] Re: [ANN] Thin 0.5.3 Purple Yogurt release — Marc-AndrCournoyer <macournoyer@...> 2008/01/19

Thin runs in a single thread, no need for a mutex there.

[#287953] Looking for code to determine length of audio files — "Rick DeNatale" <rick.denatale@...>

I'm working on a project which has the requirement to automatically

11 messages 2008/01/19

[#287971] Ruby with Mac OS X — Tj Superfly <nonstickglue@...>

Hello everyone,

22 messages 2008/01/19
[#287976] Re: Ruby with Mac OS X — "Hassan Schroeder" <hassan.schroeder@...> 2008/01/19

On Jan 18, 2008 8:37 PM, Tj Superfly <nonstickglue@verizon.net> wrote:

[#287978] Re: Ruby with Mac OS X — Tj Superfly <nonstickglue@...> 2008/01/19

She can't find the run file.

[#287979] Re: Ruby with Mac OS X — "Hassan Schroeder" <hassan.schroeder@...> 2008/01/19

On Jan 18, 2008 9:25 PM, Tj Superfly <nonstickglue@verizon.net> wrote:

[#287980] Re: Ruby with Mac OS X — Tj Superfly <nonstickglue@...> 2008/01/19

Hassan Schroeder wrote:

[#287985] Re: Ruby with Mac OS X — "s.ross" <cwdinfo@...> 2008/01/19

[#288000] Re: Ruby with Mac OS X — Tj Superfly <nonstickglue@...> 2008/01/19

Okay, we've got programs running!

[#288008] Re: Ruby with Mac OS X — "Windham, Kristopher R." <kriswindham@...> 2008/01/19

not sure what you are trying to do there..

[#288009] Re: Ruby with Mac OS X — Tj Superfly <nonstickglue@...> 2008/01/19

Well, I'm using it with arrays, here's a clip of the code that I'm

[#288133] Announcing Revactor: an Actor model implementation for Ruby 1.9 — "Tony Arcieri" <tony@...>

I'm pleased to announce the initial public availability of Revactor, an

25 messages 2008/01/21

[#288258] why must I initialize this variable? — matt@... (matt neuburg)

Here's a simple variable initialization / scope question. In the

14 messages 2008/01/22

[#288290] Determine where a method is being called from? — Ben Johnson <bjohnson@...>

Is it possible to determine if a public instance method is being called

11 messages 2008/01/22

[#288303] How to improve this code? — Jair Rillo Junior <jrjuniorsp@...>

Hi guys,

16 messages 2008/01/23

[#288308] Learning to build a MUD — Sean Dolbec <helbuns@...>

13 messages 2008/01/23

[#288348] Parsing HTML / following links etc — Dan Cuddeford <dancudds@...>

Hello all,

11 messages 2008/01/23

[#288391] Scripts run using load in "for" loop run out of order — Fa Sidd <siddiqifh@...>

Hi

13 messages 2008/01/23

[#288626] Making Change (#154) — Ruby Quiz <james@...>

The three rules of Ruby Quiz:

106 messages 2008/01/25
[#288653] Re: [QUIZ] Making Change (#154) — Dominik Honnef <dominikho@...> 2008/01/25

On [Sat, 26.01.2008 00:50], Ruby Quiz wrote:

[#288656] Re: [QUIZ] Making Change (#154) — "Jes俍 Gabriel y Gal疣" <jgabrielygalan@...> 2008/01/25

On Jan 25, 2008 9:53 PM, Dominik Honnef <dominikho@gmx.net> wrote:

[#288658] Re: [QUIZ] Making Change (#154) — James Gray <james@...> 2008/01/25

On Jan 25, 2008, at 3:09 PM, Jes=FAs Gabriel y Gal=E1n wrote:

[#288692] regular expression negate a word (not character) — Summercool <Summercoolness@...>

24 messages 2008/01/26

[#288773] Extracting Data from a Webpage — Tj Superfly <nonstickglue@...>

Hello everyone.

17 messages 2008/01/27

[#288847] Treetop parser (or PEG in general?) questions — Phrogz <phrogz@...>

I've been looking for something like treetop for a while now. Very

19 messages 2008/01/28

[#288916] Final Two Quizzes — James Gray <james@...>

As most of you know, we have two Ruby Quiz problems left (http://groups.google.com/group/comp.lang.ruby/msg/6f46393932c22e49

32 messages 2008/01/28
[#288978] Re: Final Two Quizzes — fedzor <fedzor@...> 2008/01/28

[#288980] Re: Final Two Quizzes — James Gray <james@...> 2008/01/28

On Jan 28, 2008, at 5:38 PM, fedzor wrote:

[#289016] Re: Final Two Quizzes — fedzor <fedzor@...> 2008/01/29

[#289026] Re: Final Two Quizzes — James Gray <james@...> 2008/01/29

On Jan 29, 2008, at 6:33 AM, fedzor wrote:

[#289048] Re: Final Two Quizzes — "Thomas Wieczorek" <wieczo.yo@...> 2008/01/29

Thank you for the many quizzes. I came to Ruby through a blog entry

[#289051] Re: Final Two Quizzes — James Gray <james@...> 2008/01/29

On Jan 29, 2008, at 3:23 PM, Thomas Wieczorek wrote:

[#289057] Re: Final Two Quizzes — Dominik Honnef <dominikho@...> 2008/01/29

On [Wed, 30.01.2008 06:34], James Gray wrote:

[#288973] Treetop Email Parser — Phrogz <phrogz@...>

(I would post this to the treetop mailing list...except there doesn't

14 messages 2008/01/28

[#289018] Proposal/RFQ: Hash#values/keys with block — "Dirk Traulsen" <dirk.traulsen@...>

Hi!

10 messages 2008/01/29

[#289046] newbie file write problem — Lars Zeb <larzeb@...>

This is my first attempt at ruby. I've written a class (SicCode) which

15 messages 2008/01/29

[#289060] For Loop question — Mark Mr <pimea.mark@...>

ok basically i cant quite figure out how to do a for loop i want in

13 messages 2008/01/29

[#289196] counting the number of repititions in an array — Adam Akhtar <adamtemporary@...>

Hi if i want to count the number of times values are repeated in an

10 messages 2008/01/30

[#289206] Ruby Puzzle Challenge — Wyatt Greene <greenewm@...>

Write a Ruby program that prints out the numbers 1 to 100, one number

14 messages 2008/01/30

[#289210] Learn how to program — "Regnum@... Regnum@..." <regnum@...>

Hi!, i want to learn how to program.Is ruby a good option?. Thanks.

30 messages 2008/01/30
[#289351] Re: Learn how to program — longint <michael.mello@...> 2008/01/31

On Jan 30, 4:30=A0pm, "Reg...@argentina.com Reg...@argentina.com"

[#289401] Re: Learn how to program — "Michael Bevilacqua-Linn" <michael.bevilacqualinn@...> 2008/02/01

IMHO,

[#289286] can ik make this more beautifull? — Remco Hh <remco@...>

nowadays i try to improve my coding style to produce nicer and beter

17 messages 2008/01/31

[#289355] Gedankenexperiment on method duck type safety — Tim Connor <timocratic@...>

*braces for the flames to follow*

13 messages 2008/01/31

[#289364] GUI for a newbie — Matthew Borgeson <hibridmatthias@...>

Hello All-

28 messages 2008/01/31

[#289375] how do you install a local gem? — 7stud -- <bbxx789_05ss@...>

Hi,

20 messages 2008/01/31
[#289378] Re: how do you install a local gem? — Stefano Crocco <stefano.crocco@...> 2008/01/31

Alle Thursday 31 January 2008, 7stud -- ha scritto:

[#289386] Re: how do you install a local gem? — 7stud -- <bbxx789_05ss@...> 2008/01/31

Stefano Crocco wrote:

[#289388] Re: how do you install a local gem? — Stefano Crocco <stefano.crocco@...> 2008/01/31

Alle Thursday 31 January 2008, 7stud -- ha scritto:

[#289398] Re: how do you install a local gem? — 7stud -- <bbxx789_05ss@...> 2008/02/01

Stefano Crocco wrote:

[SUMMARY] Studying Blackjack (#151)

From: Ruby Quiz <james@...>
Date: 2008-01-10 17:18:47 UTC
List: ruby-talk #286961
The solutions took two totally different approaches this week.  Some
exhaustively searched the possible hands a dealer can have.  The search space
isn't too big in this case so that's a viable approach that gives exact results.

Others chose to solve the problem with a simulation.  This approach is very
simple:  deal a ton of hands, play them out by the dealer rules, and keep track
of the results.  If you simulate enough hands, this approach should zero in on
the actual numbers.

Let's see if the two approaches agree.  Here are the expected results from Eric
I.'s exhaustive search:

	upcard       bust       17       18       19       20       21  natural
	------    -------  -------  -------  -------  -------  -------  -------
	     2 |   35.33%   13.94%   13.33%   13.07%   12.40%   11.93%    0.00%
	     3 |   37.48%   13.28%   13.07%   12.46%   12.18%   11.54%    0.00%
	     4 |   39.85%   13.07%   12.02%   12.10%   11.64%   11.31%    0.00%
	     5 |   42.25%   12.10%   12.28%   11.73%   10.90%   10.73%    0.00%
	     6 |   42.21%   16.62%   10.62%   10.67%   10.12%    9.75%    0.00%
	     7 |   26.11%   37.05%   13.82%    7.80%    7.88%    7.34%    0.00%
	     8 |   24.16%   12.97%   36.12%   12.90%    6.89%    6.96%    0.00%
	     9 |   23.09%   12.09%   11.20%   35.41%   12.11%    6.10%    0.00%
	    10 |   21.32%   11.29%   11.22%   11.30%   33.56%    3.55%    7.77%
	   ace |   11.59%   12.85%   13.09%   13.02%   13.12%    5.28%   31.07%

Now here are the same results from Chris Lowis's simulation code (with the
number of games increased to 100,000):

	Upcard  Bust    17      18      19      20      21      Natural
	c2      35.02%  14.06%  13.36%  13.12%  12.53%  11.90%   0.00%
	c3      37.24%  13.47%  12.94%  12.67%  12.27%  11.42%   0.00%
	c4      39.29%  13.25%  12.58%  12.16%  11.59%  11.13%   0.00%
	c5      41.78%  12.29%  12.24%  11.78%  11.20%  10.71%   0.00%
	c6      42.05%  16.36%  10.75%  10.69%  10.14%  10.02%   0.00%
	c7      25.97%  37.00%  13.89%   7.83%   7.75%   7.57%   0.00%
	c8      24.52%  12.75%  35.93%  12.75%   6.94%   7.10%   0.00%
	c9      22.86%  12.00%  11.97%  35.02%  12.08%   6.07%   0.00%
	ct      21.43%  11.04%  11.11%  11.05%  34.37%   3.36%   7.63%
	cj      21.39%  11.11%  11.15%  11.17%  34.07%   3.44%   7.67%
	cq      21.16%  10.97%  11.30%  11.13%  34.19%   3.47%   7.79%
	ck      21.23%  11.27%  11.13%  11.24%  34.07%   3.49%   7.58%
	ca      13.13%  12.88%  12.86%  12.71%  12.71%   4.95%  30.77%

As you can see, they are very close to each other.

There are advantages to both approaches and I had a hard time picking what to
talk about in this summary.  If accuracy is your biggest concern, you're
probably better off sticking with the exhaustive search.  However, simulation
gets very close results, is probably a little easier to code up, and would be an
option even if the search space was much larger (though the results may be less
accurate for that).

Given that, I'm going to show a simulation solution here.  If you are more
interested in the exhaustive search, Eric I.'s code is well commented and easy
to read.

The solution I want to look at below is Sander Land's first submission.  It's
not perfect and I'll try to point out the problems as they come up, but the code
is a very straight-forward simulation with a few good tricks in it.  I think
that makes it worth a read through.

Sander's solution is written for Ruby 1.9, so it doesn't run without
modification on earlier versions.  I'll point out the new features as we go and
that will give us a chance to see how some of the new stuff gets used.

Here's the start of the code:

	class Array
	  def score
	    sort.inject(0){|s,c| s+c > 21 && c==11 ? s+1 : s+c }
	  end
	end
	
	# ...

This is Sander's code for valuing Blackjack hands.  Sander's notion of a hand is
simply an Array of 2 through 11 Integers.  Blackjack isn't affected by suits, so
this code doesn't bother to track them.

This code counts drops an ace an to 1 if counting it as 11 would bust the hand. 
Note that the hand is first sorted to push aces to the end and make sure they
are counted last.  Everything else gets the normal card value added on to a
running total.

While, this code gets very close, it doesn't work in all cases.  Consider a hand
containing a ten, an ace, and another ace.  The code will count the ten, add
eleven to it, because it wouldn't bust the hand, then add one more.  This gives
a final total of twenty two, instead of the correct count of twelve.  At least
three solutions made the same error.

One way to fix the code is:

	class Array
	  def score
	    sort.each_with_index.inject(0){|s,(c,i)|
	      s+c > 21 - (size - (i + 1)) && c==11 ? s+1 : s+c
	    }
	  end
	end

This does the same thing, but reduces the hand limit for each card we have left
to count once we reach the aces.  They are sorted to the end so we know we only
have aces left and we could choose to count them as one each.  Note that I used
a 1.9 feature here of calling each_with_index() without a block to get an
Enumerator object.  That allows me to inject() over the values with their index.

Here's the next bit of Sander's code:

	# ...
	
	unless ARGV[0]
	 (2..11).each{|n| puts `ruby1.9 #{__FILE__} #{n}`}
	 exit
	end
	
	# ...

I thought this was a great trick.  As you are about to see, the rest of the code
is written such that it only worries about a single upcard.  That simplified the
code, but it doesn't let us see all of the results at once.  To fix that, Sander
just calls his own program once for each upcard, when one isn't provided.  It's
a recursive process call, so to speak.

The downside here is that this code didn't run for me as written.  I call my
ruby 1.9 install ruby_dev, so the hardcoded name bit me.  A more portable way to
get the name would be:

	unless ARGV[0]
	 require "rbconfig"
	 (2..11).each{|n|
	   puts `#{Config::CONFIG["ruby_install_name"]} #{__FILE__} #{n}`
	 }
	 exit
	end

Let's more on.  Here's the code that reads the upcard and number of decks to
use:

	# ...
	
	puts "upcard: #{upcard = ARGV[0].to_i}"
	NDECKS = (ARGV[1]||2).to_i
	CARDS = (((2..11).to_a+[10]*3)*4*NDECKS).tap{|c|
	          c.delete_at c.index(upcard)
	        }
	
	# ...

We know we have an upcard if we made it this far, since the code before calls us
with one when the user doesn't pass one in.  That makes it safe to read in at
this point.  The number of decks is also read if available, or the default of
two is assigned when it's not.

The last bit of this code builds a full deck.  It does that by generating the
values 2 through 11, adding on three additional 10 values for the face cards,
multiplying the whole set by 4 for the suits, and multiplying that standard deck
by the count of decks we want to play with.

This gives us the number of requested full decks, but we need to remove the
already used upcard.  That's what the tap() call does and it's new in Ruby 1.9. 
delete_at() returns the element deleted, but we need the resulting deck back
instead, to store it for future use.  That's what tap() is for.  You can tap()
into a chain of calls to run some code, but you still get the object itself back
as the result of the tap() call.

We're now ready to run the simulation:

	# ...
	
	score_count = [0]*27
	cards = []
	N=(ARGV[2]||100_000).to_i
	N.times{
	 cards = CARDS.dup.shuffle if cards.size < 17
	 dealer = [upcard]
	 dealer << cards.pop while dealer.score < 17
	 score_count[dealer.score] += 1
	}
	
	# ...

Believe it or not, that's the entire simulation code.

It begins by creating an Array to hold the tally of scores, indexed by hand
total.  It's interesting to look at the size of this Array, initialized to
twenty seven 0's.  That makes the highest Array index twenty six, which is the
largest hand a dealer will ever bust with.  That is, they can draw a face card
when hitting a sixteen (a ace would count as one in this case).  Indices zero
through sixteen won't be touched, since a dealer won't stop on these totals.

The rest of this code creates an Array to hold the cards left, and decides how
many rounds to run the simulation for based on user input or a default.  The
simulation loop then starts, doing just four things:  deal and shuffle() (added
in Ruby 1.9) a fresh deck if we may not have enough cards to deal another hand
(seventeen aces at minimum), initialize a hand with the upcard, deal additional
cards until we crest a total of seventeen, and record the final hand total.

The good point about this code is that it reuses the deck for as long as it can.
This simulation gets fairly accurate results by trying many possible hands with
each trip through the deck.

One downside is that it doesn't distinguish between a total of twenty one and a
natural.  You could fix this by dedicating some slot in the score Array for that
(or switching to a score Hash of Hash.new(0)) and adding a check for this
special hand after the hitting code.

With the results tallied, printing is all that remains:

	# ...
	
	puts %w[17 18 19 20 21 bust].join('     ')
	puts (score_count[17..21] << score_count[22..-1].inject(:+)).
	     map{|x| '%-4.1f%%  ' % (100.0*x / N )}.join

The only real point of interest in this code is another 1.9 idiom.  inject() has
be enhanced so that it can now take a Symbol argument for the method to call in
the block.  That greatly simplifies the common summing case, as we see here.

Again the other solutions and approaches to this problem were all interesting. 
Do take the time to look through them.

My thanks to all the card players that decided to ante up for this quiz.

Tomorrow we will continue the Blackjack theme..

In This Thread

Prev Next