[#309764] The Rubyist -- a semi-monthly magazine for Rubyists AVAILABLE NOW — "Jeremy McAnally" <jeremymcanally@...>

Hello all,

9 messages 2008/08/01

[#309802] Using array.select with grep — Milo Thurston <knirirr@...>

Using irb I set up the following arrays:

16 messages 2008/08/01

[#309821] About circular dependencies in RubyGems (the library). And about the order in $". — "Erik Veenstra" <erikveen@...>

Hi,

13 messages 2008/08/01

[#309824] Determining MAC address — "Glen Holcomb" <damnbigman@...>

What would be the best (clean, cross-platform) way of determining the MAC

12 messages 2008/08/01

[#309867] Capturing shell command output and success? — "Kyle Schmitt" <kyleaschmitt@...>

I know shell commands have beaten to death on this list, but searching

10 messages 2008/08/01

[#309878] Help finding this syntax error — Patrick Li <patrickli_2001@...>

<code>

14 messages 2008/08/01

[#309903] unit testing advice — Shadowfirebird <shadowfirebird@...>

Forgive me if this is a stupid question.

58 messages 2008/08/01
[#309905] Re: unit testing advice — "Gregory Brown" <gregory.t.brown@...> 2008/08/01

On Fri, Aug 1, 2008 at 5:31 PM, Shadowfirebird <shadowfirebird@gmail.com> wrote:

[#309907] Re: unit testing advice — Shadowfirebird <shadowfirebird@...> 2008/08/01

Many thanks. Clearly I'm going to have to go back to research mode on

[#309916] Re: unit testing advice — Phlip <phlip2005@...> 2008/08/02

Shadowfirebird wrote:

[#309947] Re: unit testing advice — "David A. Black" <dblack@...> 2008/08/02

Hi --

[#309948] Re: unit testing advice — Phlip <phlip2005@...> 2008/08/02

David A. Black wrote:

[#309949] Re: unit testing advice — "David A. Black" <dblack@...> 2008/08/02

Hi --

[#309960] Re: unit testing advice — Shadowfirebird <shadowfirebird@...> 2008/08/02

I certainly "get" the idea that it's better to write the tests first.

[#309966] Re: unit testing advice — "David A. Black" <dblack@...> 2008/08/02

Hi --

[#309979] Re: unit testing advice — James Britt <james.britt@...> 2008/08/02

David A. Black wrote:

[#309983] Re: unit testing advice — "David A. Black" <dblack@...> 2008/08/02

Hi --

[#310011] Re: unit testing advice — James Britt <james.britt@...> 2008/08/03

David A. Black wrote:

[#310018] Re: unit testing advice — "David A. Black" <dblack@...> 2008/08/03

Hi --

[#310050] RubyConf 2008 call for talk proposals — Richard Kilmer <rich@...>

Ruby Central is pleased to announce that we are accepting

11 messages 2008/08/04

[#310127] hash code reference - this only returns 'nil' please help me — Mmcolli00 Mom <mmc_collins@...>

Please tell me what I am doing wrong. i thought h[ErrFieldID] would work

12 messages 2008/08/04
[#310128] Re: hash code reference - this only returns 'nil' please help me — Sebastian Hungerecker <sepp2k@...> 2008/08/04

Mmcolli00 Mom wrote:

[#310131] Re: hash code reference - this only returns 'nil' please help me — Mmcolli00 Mom <mmc_collins@...> 2008/08/04

Sebastian Hungerecker wrote:

[#310154] Matrix class: How to set a single element ? — Marcio Braga <mbraga0001@...>

simple code:

11 messages 2008/08/05

[#310159] State of the Onion — "M. Edward (Ed) Borasky" <znmeb@...>

http://www.gcn.com/online/vol1_no1/46724-1.html

36 messages 2008/08/05
[#310179] Re: State of the Onion — "Michael T. Richter" <ttmrichter@...> 2008/08/05

On Tue, 2008-08-05 at 11:05 +0900, M. Edward (Ed) Borasky wrote:

[#310190] Re: State of the Onion — "Martin DeMello" <martindemello@...> 2008/08/05

On Mon, Aug 4, 2008 at 8:48 PM, Michael T. Richter <ttmrichter@gmail.com> wrote:

[#310217] perl and the culture of libraries — "Martin DeMello" <martindemello@...>

http://blog.jrock.us/articles/You%20are%20missing%20the%20point%20of%20Perl.pod

60 messages 2008/08/05
[#310219] Re: perl and the culture of libraries — "Peter Fitzgibbons" <peter.fitzgibbons@...> 2008/08/05

I'm on board.

[#310223] Re: perl and the culture of libraries — "Martin DeMello" <martindemello@...> 2008/08/05

On Tue, Aug 5, 2008 at 6:04 AM, Peter Fitzgibbons

[#310228] Re: perl and the culture of libraries — "Peter Fitzgibbons" <peter.fitzgibbons@...> 2008/08/05

Sounds like some changes to rubygems and it's toolset.

[#310232] Re: perl and the culture of libraries — "Martin DeMello" <martindemello@...> 2008/08/05

On Tue, Aug 5, 2008 at 6:31 AM, Peter Fitzgibbons

[#310237] Re: perl and the culture of libraries — "Peter Fitzgibbons" <peter.fitzgibbons@...> 2008/08/05

Isn't it true, though, that rubygems are the defacto distribution model for

[#310240] Re: perl and the culture of libraries — "Martin DeMello" <martindemello@...> 2008/08/05

On Tue, Aug 5, 2008 at 7:05 AM, Peter Fitzgibbons

[#310249] Re: perl and the culture of libraries — Shadowfirebird <shadowfirebird@...> 2008/08/05

I don't wish to be critical (I really don't! That's not just a way of

[#310312] Re: perl and the culture of libraries — David Masover <ninja@...> 2008/08/06

On Tuesday 05 August 2008 07:56:22 Martin DeMello wrote:

[#310339] Re: perl and the culture of libraries — Shadowfirebird <shadowfirebird@...> 2008/08/06

I've seen some things posted in this thread (overnight for me) that I

[#310295] State of Ruby 1.8.6? — Jeff <cohen.jeff@...>

Can anyone provide an update to the state of Ruby 1.8.6?

19 messages 2008/08/05
[#310305] Re: State of Ruby 1.8.6? — Alex Fenton <alex@...> 2008/08/06

Jeff wrote:

[#310314] is there a way to AutoParse a string to another type - e.g. if a Date format then date, else if integer than Integer etc ????? — "Greg Hauptmann" <greg.hauptmann.ruby@...>

Hi,

7 messages 2008/08/06

[#310358] Threaded IO trouble — "Michal Suchanek" <hramrach@...>

Hello

13 messages 2008/08/06

[#310393] An introduction, in about 50 lines of Ruby. — Alexei Broner <lahgyk@...>

Hi, I'm Lex. I've been Rubying for a few months now and can't get

47 messages 2008/08/06
[#310582] Re: An introduction, in about 50 lines of Ruby. — Alexei Broner <lahgyk@...> 2008/08/08

You guys are no fun. I didn't even get "that's not really recursive,

[#310584] Re: An introduction, in about 50 lines of Ruby. — Yukihiro Matsumoto <matz@...> 2008/08/08

Hi,

[#310591] Re: An introduction, in about 50 lines of Ruby. — Trans <transfire@...> 2008/08/08

[#310639] Re: An introduction, in about 50 lines of Ruby. — James Britt <james.britt@...> 2008/08/08

Trans wrote:

[#310731] Re: An introduction, in about 50 lines of Ruby. — Trans <transfire@...> 2008/08/10

[#310740] Re: An introduction, in about 50 lines of Ruby. — James Britt <james.britt@...> 2008/08/10

Trans wrote:

[#310745] Re: An introduction, in about 50 lines of Ruby. — Trans <transfire@...> 2008/08/10

[#310413] Need help detecting overlapping ranges — Bryan Richardson <btrichardson@...>

Hello all,

18 messages 2008/08/06
[#310429] Re: Need help detecting overlapping ranges — "Martin DeMello" <martindemello@...> 2008/08/06

On Wed, Aug 6, 2008 at 11:48 AM, Bryan Richardson

[#310518] Most compact command for associate array 'totalling'? — John Pritchard-williams <monojohnny@...>

Ok - in 'awk' you can do this: (Where 'array' is empty initially)

10 messages 2008/08/07

[#310531] Ruby 1.8.7-p71 / 1.8.6-p286 released (Security Fix) — Urabe Shyouhei <shyouhei@...>

At last.

13 messages 2008/08/08

[#310540] Ruby IDE — "Mayuresh Kathe" <kathe.mayuresh@...>

Hello,

52 messages 2008/08/08
[#310600] Re: Ruby IDE — "Jayson Williams" <williams.jayson@...> 2008/08/08

I like working in NetBeans, but even on the Quad processor computer at

[#310603] Re: Ruby IDE — Dana Merrick <dmerrick@...> 2008/08/08

Jayson Williams wrote:

[#310610] Re: Ruby IDE — "Eric Schulte" <schulte.eric@...> 2008/08/08

To piggy-back on and emphasize the importance of Dana's point,

[#310719] Re: Ruby IDE — "Piyush Ranjan" <piyush.pr@...> 2008/08/09

I second emacs

[#310725] Re: Ruby IDE — "M. Edward (Ed) Borasky" <znmeb@...> 2008/08/09

On Sun, 2008-08-10 at 05:25 +0900, Piyush Ranjan wrote:

[#310726] Re: Ruby IDE — Matt Lawrence <matt@...> 2008/08/09

On Sun, 10 Aug 2008, M. Edward (Ed) Borasky wrote:

[#310733] Re: Ruby IDE — Joshua Ballanco <jballanc@...> 2008/08/10

Matt Lawrence wrote:

[#310814] Re: Ruby IDE — "Martin DeMello" <martindemello@...> 2008/08/11

On 8/9/08, Joshua Ballanco <jballanc@gmail.com> wrote:

[#310580] Non-Threaded Timeout? — Bryan Richardson <btrichardson@...>

Hell all,

30 messages 2008/08/08

[#310837] gem (update ?) problem — Peter Krieg <pk.hot@...>

Hello,

21 messages 2008/08/11

[#310865] Sort array by two attributes? (like sql "order by A, B") — Max Williams <toastkid.williams@...>

IN sql we can pass two arguments to the 'order by' component, and it

9 messages 2008/08/11

[#310921] Problems with accessing directory defined in ENV variables — Thomas Luedeke <thomas.luedeke@...>

Ruby is giving me pure hell trying to access directories on a mounted

10 messages 2008/08/11

[#310947] Sigh! I'm depressed. Debian vs Ruby and the backtick and subshells — John Carter <john.carter@...>

I have just strace'd weird bug to it's root cause.

20 messages 2008/08/12

[#310950] Ruby 1.9: What to Expect by Sam Ruby @ OSCON 2008 Slide Deck Adapted S6/S9 (Single-Web Page) Version — "Gerald Bauer" <geraldbauer2007@...>

Hello,

15 messages 2008/08/12
[#311073] Re: Ruby 1.9: What to Expect by Sam Ruby @ OSCON 2008 Slide Deck Adapted S6/S9 (Single-Web Page) Version — Iki Baz Castillo <ibc@...> 2008/08/12

El Martes, 12 de Agosto de 2008, Gerald Bauer escribi=F3:

[#311080] Re: Ruby 1.9: What to Expect by Sam Ruby @ OSCON 2008 Slide Deck Adapted S6/S9 (Single-Web Page) Version — "Jeremy Kemper" <jeremy@...> 2008/08/12

On Tue, Aug 12, 2008 at 1:36 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote=

[#311085] Re: Ruby 1.9: What to Expect by Sam Ruby @ OSCON 2008 Slide Deck Adapted S6/S9 (Single-Web Page) Version — Iki Baz Castillo <ibc@...> 2008/08/12

El Martes, 12 de Agosto de 2008, Jeremy Kemper escribi=F3:

[#311086] Re: Ruby 1.9: What to Expect by Sam Ruby @ OSCON 2008 Slide Deck Adapted S6/S9 (Single-Web Page) Version — Iki Baz Castillo <ibc@...> 2008/08/12

El Martes, 12 de Agosto de 2008, I=F1aki Baz Castillo escribi=F3:

[#310988] what do you think of this code? — "Ben Aurel" <ben.aurel@...>

hi

16 messages 2008/08/12
[#310992] Re: what do you think of this code? — Stefano Crocco <stefano.crocco@...> 2008/08/12

On Tuesday 12 August 2008, Ben Aurel wrote:

[#311235] Mutithreading to implement near 7000 to 10000 mssage per min — Kaja Mohaideen <kajamohaideen_2003@...>

Hello,

10 messages 2008/08/14

[#311256] Idiom of removing a particular character from a String? — "Lorenzo E. Danielsson" <danielsson.lorenzo@...>

Hi all,

12 messages 2008/08/14

[#311390] Not So Random (#173) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

20 messages 2008/08/15

[#311427] BDD and TDD - What are they for? — "Clinton D. Judy" <cdj@...>

I'd like someone to give me very basic reasons for why I need Behavior

42 messages 2008/08/15
[#311655] Re: BDD and TDD - What are they for? — paron <rphillips@...> 2008/08/18

On Aug 16, 11:27=A0am, Eleanor McHugh <elea...@games-with-brains.com>

[#311469] firefox html, my downloaded html and firebug html different? — Adam Akhtar <adamtemporary@...>

Hi Im a relatively new rubyist and programmer in general and currently

10 messages 2008/08/16

[#311517] I wish to learn Ruby ,can anyone teach me??? — Amitanshu Gour <amitanshu_gour@...>

I am a beginner in Programming and don't have much idea about any

10 messages 2008/08/16

[#311523] writing if statement in one line with elsif condition — "Luiz Vitor Martinez Cardoso" <grabber@...>

I'm trying to convert it:

10 messages 2008/08/17

[#311553] Matrix: Need help to understand this behavior — Marcio Braga <mbraga0001@...>

a=[1]

10 messages 2008/08/17

[#311571] Security in use of contants — Kless <jonas.esp@...>

Is secure use constants?

40 messages 2008/08/17

[#311668] Hash#each vs Hash#each_pair — "Patrick Doyle" <wpdster@...>

Sorry if this is a FAQ, but I'm curious to learn the rationale behind

12 messages 2008/08/18

[#311803] Cut a string if length > n — Pål Bergström <pal@...>

What's the best way to cut a string if the length is above n characters?

15 messages 2008/08/19

[#311843] Shortest Ruby crash #49 — Limo Driver <melezov@...>

My contribution to the contest:

15 messages 2008/08/19

[#311937] A Mascot... — "Mayuresh Kathe" <kathe.mayuresh@...>

Hi,

100 messages 2008/08/20
[#311969] Re: A Mascot... — Phlip <phlip2005@...> 2008/08/20

Mayuresh Kathe wrote:

[#311979] Re: A Mascot... — "Mayuresh Kathe" <kathe.mayuresh@...> 2008/08/20

On Wed, Aug 20, 2008 at 5:51 PM, Phlip <phlip2005@gmail.com> wrote:

[#311980] Re: A Mascot... — "Michal Suchanek" <hramrach@...> 2008/08/20

On 20/08/2008, Mayuresh Kathe <kathe.mayuresh@gmail.com> wrote:

[#311983] Re: A Mascot... — "Clinton D. Judy" <cdj@...> 2008/08/20

V2hpY2ggaXMgd2h5IGEgZ3JlYXQgZ3JhcGhpYyBkZXNpZ25lciBjb3VsZCBjb21lIHVwIHdpdGgg

[#311984] Re: A Mascot... — "Mayuresh Kathe" <kathe.mayuresh@...> 2008/08/20

Clinton, what's your opinion about http://www.hexley.com/

[#312284] Re: A Mascot... — Karl von Laudermann <doodpants@...> 2008/08/22

On Aug 21, 8:26=A0pm, Joshua Ballanco <jball...@gmail.com> wrote:

[#312302] Re: A Mascot... — "Pablo Q." <paqs140482@...> 2008/08/22

I like it! :D, but If you haven=B4t read all post before please do it,

[#312305] Re: A Mascot... — "Gregory Brown" <gregory.t.brown@...> 2008/08/22

On Fri, Aug 22, 2008 at 1:22 PM, Pablo Q. <paqs140482@gmail.com> wrote:

[#311950] array of hashes - need to iterate and calulate stats but how — Adam Akhtar <adamtemporary@...>

I making a script which generates some basic stats for completed

19 messages 2008/08/20
[#311952] Re: array of hashes - need to iterate and calulate stats but how — Lex Williams <etaern@...> 2008/08/20

something like this :

[#311962] Re: array of hashes - need to iterate and calulate stats but how — "David A. Black" <dblack@...> 2008/08/20

Hi --

[#312026] Iterating through a hash — "Brian Ross" <p.brian.ross@...>

How can I iterate through a hash so that each key is modified and saved into

13 messages 2008/08/20

[#312048] unintuitive language feature (exclamation functions) — Nick Brown <ruby-forum.com@...>

I was surprised to discover that the code

20 messages 2008/08/20
[#312050] Re: unintuitive language feature (exclamation functions) — "F. Senault" <fred@...> 2008/08/20

Le 20 ao皦 2008 21:45, Nick Brown a 馗rit :

[#312054] Re: unintuitive language feature (exclamation functions) — Nick Brown <ruby-forum.com@...> 2008/08/20

F. Senault wrote:

[#312068] "num in [1,2,3,4]" in a cool way? — Iñaki Baz Castillo <ibc@...>

Hi, AFAIK in Ruby the only (or the "coolest") way to do something as:

17 messages 2008/08/20

[#312360] DoS vulnerability in REXML — Shugo Maeda <shugo@...>

Hello,

16 messages 2008/08/23
[#312384] Re: [ANN] DoS vulnerability in REXML — James Britt <james.britt@...> 2008/08/23

Shugo Maeda wrote:

[#312391] Re: [ANN] DoS vulnerability in REXML — "Christopher Dicely" <cmdicely@...> 2008/08/23

On Sat, Aug 23, 2008 at 9:28 AM, James Britt <james.britt@gmail.com> wrote:

[#312411] Re: [ANN] DoS vulnerability in REXML — James Britt <james.britt@...> 2008/08/23

Christopher Dicely wrote:

[#312378] Uptime Since... (#174) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

26 messages 2008/08/23

[#312388] Why 'if 0' succeeds in Ruby — Phlip <phlip2005@...>

Rubiods:

21 messages 2008/08/23

[#312401] Open file, get first line, delete first line close file — Richard Schneeman <thedickster@...>

Hey, i'm trying to open a file, get the first line of the file, delete

17 messages 2008/08/23

[#312482] Moving all files in a folder to another hard drive — SpringFlowers AutumnMoon <summercoolness@...>

I have some code below to move all files in a folder to another hard

14 messages 2008/08/24

[#312573] Regular Expressions — Newb Newb <hema@...>

I have a image url like this <img src

16 messages 2008/08/25

[#312655] Unraveling binary data out of the proc filesystem on Solaris — Daniel Berger <djberg96@...>

Hi all,

13 messages 2008/08/25
[#312679] Re: Unraveling binary data out of the proc filesystem on Solaris — "Heesob Park" <phasis@...> 2008/08/26

Hi,

[#312734] Passing a block with define_method — "James Coglan" <jcoglan@...>

Hi list,

14 messages 2008/08/26

[#312739] MissingSourceFile: no such file to load -- sqlite3/database — Jo縊 Maca兊a <joao.macaiba@...>

Hi,

11 messages 2008/08/26

[#312792] Chris Pine Tutorial 99 Bottles of Beer Program — danielj <sleepingindian@...>

Just a beginner with a question about this:

15 messages 2008/08/26

[#312795] Deaf Grandma — danielj <sleepingindian@...>

Also from the Chris Pine tutorial for beginners:

20 messages 2008/08/27

[#312825] how to avoid passing by reference and how to copy objects — Adam Akhtar <adamtemporary@...>

Hi after a bit of searching and reading im quite confused by this.

12 messages 2008/08/27

[#312839] Difficult Inheritance Problem — Toby Clemson <tobyclemson@...>

Hi all,

15 messages 2008/08/27

[#312875] encrypting password on form submit? — "Amanda .." <a.etherton@...>

Hi there, I'm trying to use a form to create a user for a site. All the

19 messages 2008/08/27

[#312906] Having a difficult time with Case statements. — Chris Bailey <christopher.sean.bailey@...>

I am trying to implement a simple frontend for a text adventure that

12 messages 2008/08/27

[#312989] specifying a network interface, with a http get request — Andrew Parlane <ajp97@...>

Hi all, I'm fairly new to Ruby but have learnt a lot in the last month

13 messages 2008/08/28
[#313071] Re: specifying a network interface, with a http get request — John Pritchard-williams <monojohnny@...> 2008/08/28

Hi Andy,

[#313182] Re: specifying a network interface, with a http get request — Andrew Parlane <ajp97@...> 2008/08/29

Hey John,

[#312990] Beginner help: Problem installing sqlite3 on Windows XP — Kasper Frederiksen <kasper@...>

Hi Everyone,

11 messages 2008/08/28

[#313031] A general doubt — Rock Roll <karoljouis@...>

If I am developing a Windows application in Ruby using FxRuby or WxRuby,

28 messages 2008/08/28
[#313038] Re: A general doubt — Michael Morin <uzimonkey@...> 2008/08/28

Rock Roll wrote:

[#313070] Use a string as a method call — Chris Bailey <christopher.sean.bailey@...>

I'm trying to come up with an efficient way of using user input as a

16 messages 2008/08/28

[#313098] Please don't flame me...why is there no "++" in Ruby again ? — John Pritchard-williams <monojohnny@...>

There's must be very good simple reason why there is no 'x++' method

24 messages 2008/08/28
[#313100] Re: Please don't flame me...why is there no "++" in Ruby again ? — Joel VanderWerf <vjoel@...> 2008/08/28

John Pritchard-williams wrote:

[#313164] Re: Please don't flame me...why is there no "++" in Ruby again ? — David Masover <ninja@...> 2008/08/29

On Thursday 28 August 2008 16:25:59 Joel VanderWerf wrote:

[#313131] Class === — "RubyTalk@..." <rubytalk@...>

I need help with === and Objects

20 messages 2008/08/29
[#313135] Re: Class === — "Patrick Doyle" <wpdster@...> 2008/08/29

and I would also like to know why

[#313196] Computing folder size - do you have something cleaner than this ? — Thibaut Barr鑽e <thibaut.barrere@...>

Hi guys,

11 messages 2008/08/29

[#313242] Where the Required Things Are (#175) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

21 messages 2008/08/29

[#313379] instance_eval/class_eval including/extending modules — Pedro Silva <ei04065@...>

Consider the following example:

14 messages 2008/08/30
[#313401] Re: instance_eval/class_eval including/extending modules — "David A. Black" <dblack@...> 2008/08/31

HI --

[#313428] Re: instance_eval/class_eval including/extending modules — Pedro Silva <ei04065@...> 2008/08/31

David, thanks for your reply.

[#313432] Re: instance_eval/class_eval including/extending modules — "David A. Black" <dblack@...> 2008/08/31

Hi --

[#313399] Difference between dir/**/* and dir/*? — Ben Johnson <bjohnson@...>

I've noticed the following when specifying a wildcard array of files:

14 messages 2008/08/31

[SUMMARY] Code Heuristics (#172)

From: "Matthew Moss" <matthew.moss@...>
Date: 2008-08-10 22:28:35 UTC
List: ruby-talk #310799
Apologies for the late summary... Very interesting techniques in the
submissions!

To begin evaluating the solutions for this quiz, I ran each solution
over the original submission text of each submission. In a way, this
would probably be the most difficult test, seeing as how the solutions
discussed themselves, with sample inputs and outputs. What parts
should be considered code and which text? If I can't figure it out, I
can't really expect an algorithm to do that. Still, it was interesting
to see the results; in particular, what I _did_ want to see, if
nothing else, was that the actual code for the submission was
identified as code.

And, except for one line, both worked well at identifying the main
body of code _as_ code.. (The solution provided by _Jesse Brown_
identified it's own last line -- `out.close` -- as text rather than
code.) The results for the discussion portions of the submissions were
mixed, mostly from false positives. After a quick examination, though,
I believe _Frederick Cheung_ was more consistent in differentiating
code from commentary.

Perhaps the reason was the approach. As Frederick described:

> I got lazy and thought I'd let ruby do the hard work. Given some
> text, I feed it through [eval] and see if an exception is raised
> or not (this does have limitations).

This seems to be a generally good approach. Most discussion will
contain words or punctuation that I would not expect to pass safely
through Ruby's parser, and so the number of incorrect categorizations
of text should be minimal. Frederick also notes the sorts of cases
that are identified incorrectly as code; a more complete solution,
perhaps integrating some ideas from solutions such as Jesse's, would
accommodate those classes. (Anyone want to write a meta-classifier?)

Let's take a look at Frederick's solution. We start with initialization:

    class CodeExtractor
       attr_reader :lines, :output

       def initialize(text)
         @output = []
         @lines = text.split(/[\r\n]/)
       end

       def extract
         while lines.any?
           process lines
         end
       end

       # ...

       def self.extract(text)
         c= CodeExtractor.new text
         c.extract
         c.format_output
         nil
       end
    end

    # Here's how I used Frederick's CodeExtractor class:
    CodeExtractor.extract(ARGF)

Pretty straightforward. A new instance of CodeExtractor is created via
the `extract` _class_ method, the input text is split on newline and
carriage-return characters and saved in `@lines`. Then the _instance_
method `extract` is called, which repeatedly calls `process` on all
remaining lines (via the accessor created by `attr_reader`) until none
remain.

So what does it mean to process?

       def process lines
         if (prefix_length = valid_code_prefix_length lines) > 0
           prefix_length.times { append_output lines.shift, true }
         else
           append_output lines.shift, false
         end
       end

       def append_output(line, code)
         @output << Struct::Line.new(line, code)
       end

A test is performed: `valid_code_prefix_length` (we'll come back to
that). Assuming the test result is positive, we remove that many lines
from `@lines` and append them to `@output` (via `lines.shift` and the
`append_output` method). Each line of text moved in this process is
wrapped in the `Line` structure, which includes a flag indicating that
these lines are code.

When `valid_code_prefix_length` returns zero, only the first line is
moved as such, the flag set to indicate that it is not code.
Eventually, all lines will have been moved from `@lines` to `@output`,
identified either as code or not.

Let's continue, looking next at `valid_code_prefix_length`:

       # returns the maximum number of lines (contiguous from
       # the start) that are valid ruby
       def valid_code_prefix_length lines
         max_valid_lines = 0
         code = ""
         lines.each_with_index do |line, index|
           code << line
           code << "\n"
           if valid_syntax? code
             max_valid_lines = index + 1
           end
         end
         return max_valid_lines
       end

`valid_code_prefix_length` takes as input an array of lines; in this
case, the input data we are processing. Adding a line at a time to
local variable `code`, it calls `valid_syntax?` to make a guess as to
whether `code` is truly code. The loop (`each_with_index`) continues
in this fashion, increasing `max_valid_lines` each time around, until
`valid_syntax?` returns false. At that time, `max_valid_lines` is
returned, indicating how many adjacent lines of code were found.

Finally, we get to `valid_syntax?`, the core of Frederick's code identification.

       def valid_syntax?(code)
         io = StringIO.new
         original_err, $stderr= $stderr, io
         eval("BEGIN {return true}\n#{code}")
         raise 'here'
       rescue Exception
         false
       ensure
         $stderr = original_err
         return false if io.length > 0
       end

The first bit here is to create a `StringIO` object: an object that
acts like an `IO` stream, but holds a string. This is assigned to
`$stderr` (while the old value is simultaneously remembered in
`original_err`). Reassigning `$stderr` allows us to examine any errors
that occur when we call `eval` on the next line. You can also see, at
the end of `valid_syntax?`, that the original value of `$stderr` is
restored, whether any error occurs or not.

So what is going on in that `eval`? Obviously, the code to test is
included, but so is `BEGIN { return true }`. I admit... I had to
search the manual for this one. What `BEGIN` does is to evaluate its
block before anything else in the file (or, in this case, the string)
is evaluated. Since the block is simply `return true`, the evaluation
exits before the argument is run.

What this means is that _`code` is parsed enough to be syntax checked,
but not actually executed_. This, of course, matches Frederick's
description in his submission. If the parse is successful, no
exceptions will be raised, and the `{ return true }` block will be
executed, causing `valid_syntax?` to return true. If the parse fails,
an exception is thrown, which we see provides a return value of false.

(From what I understand, it seems there is some redundancy here...
both in the call to `raise` and the check of `io.length`... since the
evaluation should either immediately return true from a successful
parse, or it is not successful. Still, perhaps there are cases that
Frederick ran across that necessitates this pattern?)



-- 
Matthew Moss <matthew.moss@gmail.com>

In This Thread

Prev Next