<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C L Snyder &#187; Ruby</title>
	<atom:link href="http://www.clsnyder.com/WordPress/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clsnyder.com/WordPress</link>
	<description>Kaizen with Sprezzatura</description>
	<lastBuildDate>Mon, 06 Feb 2012 22:52:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ruby Create Dictionary.html from double colon files</title>
		<link>http://www.clsnyder.com/WordPress/2008/06/20/ruby-create-dictionaryhtml-from-double-colon-files/</link>
		<comments>http://www.clsnyder.com/WordPress/2008/06/20/ruby-create-dictionaryhtml-from-double-colon-files/#comments</comments>
		<pubDate>Sat, 21 Jun 2008 03:06:40 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=220</guid>
		<description><![CDATA[This takes a text file of the form below and creates an html dictionary version with a TOC : Ambiguous Genitalia Overview:: Four major defects can cause gender confusion in the neonate: 1) female pseudohermaphroditism or adrenogentital syndrome, 2) male &#8230; <a href="http://www.clsnyder.com/WordPress/2008/06/20/ruby-create-dictionaryhtml-from-double-colon-files/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This takes a text file of the form below and creates an html dictionary version with a TOC :</p>
<p><code><br />
Ambiguous Genitalia</p>
<p>Overview:: Four major defects can cause gender confusion in the neonate: 1) female pseudohermaphroditism or adrenogentital syndrome, 2) male pseudohermaphroditism, 3) true hermaphroditism, and 4) mixed gonadal dysgenesis. A physical examination and a chromosome analysis can make the correct diagnosis with approximately 90% accuracy. Gonadal symmetry is sought for - if there is symmetry  in the gonadal findings, then the diagnosis is most likely a causative agent applied equally to both sides, such as an androgenized genetic female (=female pseudohermaphroditism) or an incompletely virilized genetic male (= male pseudohermaphroditism). Asymmetry  refers to position of one gonad to the other, relative to the external inguinal ring.  A buccal smear is felt by some  to be useful in that Barr or Chromatin bodies (indicative of the presence of the second female X chromosome) may be found. Therefore: 1) Chromatin Positive, Symmetry = Fem pseudoherm; 2) Chromatin Positive, Asymmetry =	True herm; 3) Chromatin Negative,Symmetry = Male pseudoherm; 5) Chromatin Negative, Asymmetry = Mixed gonad dysgen.			</p>
<p>Caveats:: Genetic females recognized in the neonatal period historically were raised as female no matter how virilized. This is not currently the case. In genetic males the gender assignment was traditionally based on the phallus size - anyone with an 'inadequate' phallus is raised as a female. What constitutes an inadequate phallus ? Absolute values are impossible, but the measurements are made from the dorsum to the tip of the stretched glans. The following are 'inadequate': < 2.5 cm in the term infant; < 2.0 cm in the 34 week premie; < 1.5 cm in the 30 week preemie. Additionally, a thin (< 1 to 1.5 cm diameter) phallus is of concern. An elevated or normal MIS levels signifies a functioning testis. </p>
<p>Clinical:: The following work-up of the child with AG (ambiguous genitalia) may be indicated:H &#038; P, HCG stimulation and enzyme ratios, Family pedigree, Genitogram, Maternal drug history, Cystoscopy, Buccal smear with Y fluorescence, Laparoscopy, Urinary steroids, Gonadal biopsy (longitudinal), Electrolytes,Androgen receptors (genital skin).</p>
<p>Female pseudohermaphroditism - Description:: (approximately 1/3 of all patients). This refers to 46, XX karyotype patients with severe masculinization secondary to exposure in utero to either endogenous or exogenous androgens. </p>
<p>Female pseudohermaphroditism - Etiology::The most common cause is the adrenogenital syndrome (CAH). CAH is the only cause of ambiguous genitalia that is life-threatening in the newborn period. 95% of these patients will have a 21-hydroxlyase deficiency. The gene for 21 hydroxylase has 2 alleles coded for in the Class II MHComplex, and there is therefore an A and B form of 21 OHase deficiency  - the common form is the B variant. Mutations in the A genes result in milder, later onset forms of the disease. 21 hydroxylase deficiency results in aldosterone (mineralocorticoid) and cortisol (glucocorticoid) deficiencies, resulting in salt-wasting, hypotension, and adrenal crises. Other etiologies include 3 beta and 11 hydroxylase deficiencies. The 21, 3, and 11 are all forms of congenital adrenal hyperplasia (CAH). 11Beta OHase deficiency results in mild virilism (in affected females) and hypertension. In this variant, both mineralocorticoid and steroid replacement should be provided, since steroids alone may suppress the mineralocorticoid.  </p>
<p>Female pseudohermaphroditism - Evaluation::The prenatal diagnosis of CAH can be made from DNA analysis of CVS (chorionic villous sampling) or amniotic fluid hormone levels during or after the 2nd trimester. The best way to test for CAH is to look for elevated precursors: {Deficiency,Elevated Precursor,Frequency} = [21 Hydroxylase;	17 hydroxyprogesterone;	95% cases] and [3BetaSteroid dehydrogenase; 17 hydroxypregnenolone: < 5% cases] and [11 Beta hydroxylase; 11 deoxycortisol; rare]. Clinically, prompt identification of patients with CAH is critical. Virilization is present in the female, and in the male only barely identifiable hyperpigmentation of the scrotum may be present. The history of an early or unexplained death in a sibling should alert one to the possibility. A filter-paper test for the most common defect (test is for 17 hydroxyprogesterone) is available. ECG findings of peaked T waves or arrhythmia's may be the first sign. Internal structures are normal, and only the external structures are affected (this is true because the adrenal develops after the 11th week of gestation when the internal structures are already formed). All genetically female patients have the potential to be fertile and should be raised as females. The phenotype is variable from very male appearing to milder forms. The life-threatening electrolyte abnormalities (hyperkalemia, hyponatremia) may not occur until 3 - 5 days after birth, and the adrenal crisis may not occur until 1-2 weeks of age. </p>
<p>Female pseudohermaphroditism - Treatment:: Can even be preventative, if CAH is identified prenatally - dexamethasone is given (1 - 1.5 mg, daily in 2 divided doses) to the mother, starting at the 6 - 7th week of gestation (before sexual differentiation). CVS is used to assess therapy - if the fetus is genetically male, Tx is stopped, if the fetus is female, Tx is continued until amniocentesis during 2nd trimester to confirm the Dx. Fetoscopy has been used to evaluate the external genitalia. Medical management of CAH consists of provision of glucocorticoid (hydrocortisone, 12.5 -15 mg/M2 q 8 hrs, provision of mineralocorticoid (Fludrocortisone 0.1mg q am), hydration, correction of Na, K+ abnormalities. All patients with female pseudohermaphroditism should be raised as females. Surgically, clitoral recession is done in the early neonatal period, and a cutback or flap vaginoplasty is usually done at 3 - 6 months of age. A pull-through vaginoplasty is usually delayed until about 2 years of age. Which of these is required depends on the level of entry of the vagina into the urogenital sinus.</p>
<p>Male Pseudohermaphroditism - General:: Accounts for approximately 1/3 of all patients. These patients have a 46 X,Y karyotype but deficient masculinization of the external genitalia. The effects of androgens on sexual differentiation are via receptors in the nucleus. </p>
<p>etc.......</p>
<p></code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;Enter a title&quot;</span>
title  = <span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>
dict = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC00FF; font-weight:bold;">IO</span>.<span style="color:#9900CC;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span>title <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;.txt&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> 
<span style="color:#9966CC; font-weight:bold;">if</span>
y = <span style="color:#006600; font-weight:bold;">/</span>::<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
y = y.<span style="color:#9900CC;">pre_match</span>
z=<span style="color:#006600; font-weight:bold;">/</span>::<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
z = z.<span style="color:#9900CC;">post_match</span>
dict<span style="color:#006600; font-weight:bold;">&#91;</span>y<span style="color:#006600; font-weight:bold;">&#93;</span> = z
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>::<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>title <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.htm'</span>, <span style="color:#996600;">'w'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
	f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;HTML&gt;<span style="color:#000099;">\n</span>&lt;link href='Level3_3.css' rel='stylesheet' type='text/css'&gt;&lt;HEAD&gt;&lt;TITLE&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> title <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;</span>
	f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;CENTER&gt;&lt;A NAME=<span style="color:#000099;">\&quot;</span>page_top<span style="color:#000099;">\&quot;</span>&gt;&lt;H1&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> title <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;&lt;/H1&gt;&lt;/A&gt;&quot;</span>	
  	f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;by Charles L. Snyder, MD&lt;BR&gt;<span style="color:#000099;">\n</span>&quot;</span>
	f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;/CENTER&gt;<span style="color:#000099;">\n</span>&lt;HR&gt;<span style="color:#000099;">\n</span>&quot;</span>
	dict.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">sort</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>s<span style="color:#006600; font-weight:bold;">|</span> f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;br&gt;#{s}&lt;/br&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
	f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;/CENTER&gt;<span style="color:#000099;">\n</span>&lt;HR&gt;<span style="color:#000099;">\n</span>&quot;</span>
  i=<span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">while</span> i <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>dict.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;A NAME=<span style="color:#000099;">\&quot;</span>$anchor<span style="color:#000099;">\&quot;</span>&gt;&lt;H2&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> dict.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">sort</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;&lt;/H2&gt;&lt;/A&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> dict.<span style="color:#9900CC;">values</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;&lt;BR&gt;&quot;</span>
	f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;CENTER&gt;&lt;A HREF=<span style="color:#000099;">\'</span>javascript:window.history.back()<span style="color:#000099;">\'</span>&gt;Back&lt;/A&gt;&amp;nbsp&quot;</span>
  f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;/CENTER&gt;<span style="color:#000099;">\n</span>&lt;HR&gt;<span style="color:#000099;">\n</span>&quot;</span>
  i = i <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2008/06/20/ruby-create-dictionaryhtml-from-double-colon-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Code Samples</title>
		<link>http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/</link>
		<comments>http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#comments</comments>
		<pubDate>Sat, 24 May 2008 03:11:45 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=192</guid>
		<description><![CDATA[Table of Contents: Tag cloud in ruby Excel in ruby How to find files /filetypes in Ruby Ruby Word Unscrambler Ruby converter for xml quiz files to Quizmaker Program Create a Histogram in Ruby Clean out old files with Ruby &#8230; <a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div  style="background:lightyellow">Table of Contents:</p>
<div class="toc">
<ol>
<li><a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#toc-tag-cloud-in-ruby">Tag cloud in ruby</a></li>
<li><a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#toc-excel-in-ruby">Excel in ruby</a></li>
<li><a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#toc-how-to-find-files-filetypes-in-ruby"> How to find files /filetypes in Ruby</a></li>
<li><a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#toc-ruby-word-unscrambler"> Ruby Word Unscrambler</a></li>
<li><a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#toc-ruby-converter-for-xml-quiz-files-to-quizmaker-program"> Ruby converter for xml quiz files to Quizmaker Program</a></li>
<li><a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#toc-create-a-histogram-in-ruby">Create a Histogram in Ruby</a></li>
<li><a href="http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/#toc-clean-out-old-files-with-ruby">Clean out old files with Ruby</a></li>
</ol>
</div>
</div>
<h3 id="toc-tag-cloud-in-ruby">Tag cloud in ruby</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#Tag cloud in Ruby</span>
<span style="color:#9966CC; font-weight:bold;">def</span> font_size_for_tag_cloud<span style="color:#006600; font-weight:bold;">&#40;</span>total,lowest,highest, options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> total.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#9966CC; font-weight:bold;">or</span> highest.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#9966CC; font-weight:bold;">or</span> lowest.<span style="color:#0000FF; font-weight:bold;">nil</span>?
 <span style="color:#008000; font-style:italic;">#</span>
 <span style="color:#008000; font-style:italic;"># options</span>
 maxf = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:max_font_size</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">14</span>
 minf = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:min_font_size</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">11</span>
 maxc = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:max_color</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006666;">0</span>, <span style="color:#006666;">0</span>, <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
 minc = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:min_color</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006666;">156</span>, <span style="color:#006666;">156</span>, <span style="color:#006666;">156</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
 hide_sizes = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:hide_sizes</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
 hide_colours = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:hide_colours</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#008000; font-style:italic;">#</span>
 <span style="color:#008000; font-style:italic;"># function to work out rgb values</span>
 <span style="color:#9966CC; font-weight:bold;">def</span> rgb_color<span style="color:#006600; font-weight:bold;">&#40;</span> a, b, i, x<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> i <span style="color:#006600; font-weight:bold;">&lt;</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">or</span> x <span style="color:#006600; font-weight:bold;">&lt;</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> a <span style="color:#006600; font-weight:bold;">&gt;</span> b
   a<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">-</span>b<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">floor</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
   <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">-</span>a<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">+</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">floor</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
 <span style="color:#9966CC; font-weight:bold;">end</span>
 <span style="color:#008000; font-style:italic;">#</span>
 <span style="color:#008000; font-style:italic;"># work out colours</span>
 c = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
 <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>..<span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> c <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> rgb_color<span style="color:#006600; font-weight:bold;">&#40;</span> minc<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>, maxc<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>, total, highest <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
 colors = c.<span style="color:#9900CC;">compact</span>.<span style="color:#9900CC;">empty</span>? ? minc.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘,’<span style="color:#006600; font-weight:bold;">&#41;</span> : c.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘,’<span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#008000; font-style:italic;">#</span>
 <span style="color:#008000; font-style:italic;"># work out the font size</span>
 spread = highest.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">-</span> lowest.<span style="color:#9900CC;">to_f</span>
 spread = <span style="color:#006666;">1</span>.<span style="color:#9900CC;">to_f</span> <span style="color:#9966CC; font-weight:bold;">if</span> spread <span style="color:#006600; font-weight:bold;">&lt;</span>= <span style="color:#006666;">0</span>
 fontspread = maxf.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">-</span> minf.<span style="color:#9900CC;">to_f</span>
 fontstep = spread <span style="color:#006600; font-weight:bold;">/</span> fontspread
 size = <span style="color:#006600; font-weight:bold;">&#40;</span> minf <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span> total.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">/</span> fontstep <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_i</span>
 size = maxf <span style="color:#9966CC; font-weight:bold;">if</span> size <span style="color:#006600; font-weight:bold;">&gt;</span> maxf
 <span style="color:#008000; font-style:italic;">#</span>
 <span style="color:#008000; font-style:italic;"># display the results</span>
 size_txt = <span style="color:#996600;">&quot;font-size:#{ size.to_s }px;&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> hide_sizes
 color_txt = <span style="color:#996600;">&quot;color:rgb(#{ colors });&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> hide_colours
 <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">&#91;</span> size_txt, color_txt <span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<h3 id="toc-excel-in-ruby">Excel in ruby</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> ‘win32ole’
myfile = ‘C:\Documents <span style="color:#9966CC; font-weight:bold;">and</span> Settings\Charles L.<span style="color:#9900CC;">Snyder</span>\My Documents\rvu.<span style="color:#9900CC;">xls</span>’ <span style="color:#008000; font-style:italic;">#put in your path and filename</span>
&nbsp;
excel = WIN32OLE::new<span style="color:#006600; font-weight:bold;">&#40;</span>‘excel.<span style="color:#9900CC;">Application</span>’<span style="color:#006600; font-weight:bold;">&#41;</span>
workbook = excel.<span style="color:#9900CC;">Workbooks</span>.<span style="color:#CC0066; font-weight:bold;">Open</span><span style="color:#006600; font-weight:bold;">&#40;</span>myfile<span style="color:#006600; font-weight:bold;">&#41;</span>
worksheet = workbook.<span style="color:#9900CC;">Worksheets</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#get the first worksheet - change the name if needed</span>
worksheet.<span style="color:#CC0066; font-weight:bold;">Select</span>
eor = <span style="color:#006600; font-weight:bold;">&#40;</span>worksheet.<span style="color:#9900CC;">range</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a1&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">end</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">4161</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">address</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;$&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#008000; font-style:italic;"># &gt;&gt; &quot;$B$7&quot; - end of row (eor)</span>
eoc = <span style="color:#006600; font-weight:bold;">&#40;</span>worksheet.<span style="color:#9900CC;">range</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a1&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">end</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">4121</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">address</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;$&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># puts ‘last row is ‘ + eoc</span>
eoc.<span style="color:#9900CC;">slice</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># just want the number of the last row</span>
eor.<span style="color:#9900CC;">slice</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#just want the letter of the last column</span>
col_titles = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
surgeons = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;ANDREWS,WALTER S MD&quot;</span>,<span style="color:#996600;">&quot;GATTI,JOHN MD&quot;</span>, <span style="color:#996600;">&quot;HOLCOMB,GEORGE WHIT MD&quot;</span>, <span style="color:#996600;">&quot;MURPHY,JOHN PATRICK MD&quot;</span>, <span style="color:#996600;">&quot;OSTLIE,DANIEL J MD&quot;</span>, <span style="color:#996600;">&quot;SHARP,RONALD MD&quot;</span>, <span style="color:#996600;">&quot;SNYDER,CHARLES MD&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
surgeons.<span style="color:#9900CC;">each</span>  <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>surg<span style="color:#006600; font-weight:bold;">|</span>
    counter =<span style="color:#006666;">0</span>
        worksheet.<span style="color:#CC00FF; font-weight:bold;">Range</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a:a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
        modf= f.<span style="color:#9900CC;">address</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;$&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
         <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>f.<span style="color:#9900CC;">value</span><span style="color:#006600; font-weight:bold;">&#41;</span> == surg <span style="color:#9966CC; font-weight:bold;">and</span> worksheet.<span style="color:#CC00FF; font-weight:bold;">Range</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{modf}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">offset</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">value</span> == <span style="color:#006666;">54161</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
         counter <span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span>
         <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{surg}: did  #{counter} operations or encounters during the interval&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<h3 id="toc-how-to-find-files-filetypes-in-ruby"> How to find files /filetypes in Ruby</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'find'</span>
<span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#CC00FF; font-weight:bold;">Find</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> match<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span>
    matched = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    find<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>path<span style="color:#006600; font-weight:bold;">|</span> matched <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> path <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">yield</span> path <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> matched
  <span style="color:#9966CC; font-weight:bold;">end</span>
  module_function <span style="color:#ff3333; font-weight:bold;">:match</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;">#---</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'create_tree'</span>
create_tree <span style="color:#996600;">'./'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 
  <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'Music'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'cancelled_download.MP3'</span>, 
                  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'The Snails - Red Rocket.mp3'</span>, <span style="color:#996600;">'Song contents #1'</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
                  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'The Snails - Moonfall.mp3'</span>, <span style="color:#996600;">'Song contents #2'</span><span style="color:#006600; font-weight:bold;">&#93;</span> 
                 <span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'tmp'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'empty1'</span>, <span style="color:#996600;">'empty2'</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'README'</span>, <span style="color:#996600;">'Hi there!'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'rubyprog-0.1'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'rubyprog.rb'</span>, <span style="color:#996600;">'#!/usr/bin/env ruby'</span><span style="color:#006600; font-weight:bold;">&#93;</span>, 
                         <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'README'</span>, <span style="color:#996600;">'This Ruby program is great!'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
                        <span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#---</span>
<span style="color:#008000; font-style:italic;"># Find the empty files.</span>
<span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'./'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">lstat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">size</span> == <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># =&gt; [&quot;./tmp/empty2&quot;, &quot;./tmp/empty1&quot;, &quot;./Music/cancelled_download.MP3&quot;]</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Find the MP3s.</span>
<span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'./'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> ext = <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">4</span>...<span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#93;</span>; ext <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> ext.<span style="color:#9900CC;">downcase</span> == <span style="color:#996600;">'.mp3'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; [&quot;./Music/The Snails - Red Rocket.mp3&quot;, </span>
<span style="color:#008000; font-style:italic;">#     &quot;./Music/The Snails - Moonfall.mp3&quot;, </span>
<span style="color:#008000; font-style:italic;">#     &quot;./Music/cancelled_download.MP3&quot;]</span>
&nbsp;
&nbsp;
<span style="color:#008000; font-style:italic;"># Find the README files.</span>
<span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'./'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'README'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; [&quot;./tmp/README&quot;, &quot;./rubyprog-0.1/README&quot;]</span>
<span style="color:#008000; font-style:italic;">#---</span>
<span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'./'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> 
  <span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">prune</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC0066; font-weight:bold;">p</span> == <span style="color:#996600;">&quot;./tmp&quot;</span>  
  <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">&quot;README&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># =&gt; [&quot;./rubyprog-0.1/README&quot;]</span>
<span style="color:#008000; font-style:italic;">#---</span>
must_start_with = <span style="color:#996600;">&quot;This Ruby program&quot;</span>
<span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'./'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">file</span>? <span style="color:#CC0066; font-weight:bold;">p</span>
    <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> f.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>must_start_with.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#41;</span> == must_start_with <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># =&gt; [&quot;./rubyprog-0.1/README&quot;]</span>
<span style="color:#008000; font-style:italic;">#---</span>
<span style="color:#008000; font-style:italic;"># Finds files that were probably left behind by emacs sessions.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> emacs_droppings<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> 
    <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> == ?~ <span style="color:#9966CC; font-weight:bold;">and</span> <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> != ?~<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">or</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> == ?<span style="color:#008000; font-style:italic;"># and p[-1] == ?#)</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Finds all files that are larger than a certain threshold. Use this to </span>
<span style="color:#008000; font-style:italic;"># find the files hogging space on your filesystem.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> bigger_than<span style="color:#006600; font-weight:bold;">&#40;</span>bytes, <span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">lstat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> bytes <span style="color:#006600; font-weight:bold;">&#125;</span>  
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Finds all files modified more recently than a certain number of seconds </span>
<span style="color:#008000; font-style:italic;"># ago.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> modified_recently<span style="color:#006600; font-weight:bold;">&#40;</span>seconds, <span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span>
  time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> seconds
  <span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">lstat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">mtime</span> <span style="color:#006600; font-weight:bold;">&gt;</span> time <span style="color:#006600; font-weight:bold;">&#125;</span>  
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Finds all files that haven't been accessed since they were last modified.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> possibly_abandoned<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC00FF; font-weight:bold;">Find</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>paths<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> f = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">lstat</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>; f.<span style="color:#9900CC;">mtime</span> == f.<span style="color:#9900CC;">atime</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;">#---</span></pre></td></tr></table></div>

<h3 id="toc-ruby-word-unscrambler"> Ruby Word Unscrambler</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Unscrambler
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>wordsFilename = <span style="color:#996600;">&quot;/usr/share/dict/words&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@wordsHash</span> = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>wordsFilename<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>wordsFile<span style="color:#006600; font-weight:bold;">|</span>
            wordsFile.<span style="color:#9900CC;">each_line</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>word<span style="color:#006600; font-weight:bold;">|</span>
                word.<span style="color:#CC0066; font-weight:bold;">chomp!</span>
                <span style="color:#0066ff; font-weight:bold;">@wordsHash</span><span style="color:#006600; font-weight:bold;">&#91;</span>Unscrambler::word_to_key<span style="color:#006600; font-weight:bold;">&#40;</span>word<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span>= word
            <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> Unscrambler::word_to_key<span style="color:#006600; font-weight:bold;">&#40;</span>word<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> word <span style="color:#9966CC; font-weight:bold;">unless</span> word.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">3</span>
        <span style="color:#CC0066; font-weight:bold;">array</span> = word.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">//</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">array</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC0066; font-weight:bold;">array</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">sort</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#CC0066; font-weight:bold;">array</span>.<span style="color:#9900CC;">join</span>;
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> unscramble<span style="color:#006600; font-weight:bold;">&#40;</span>word<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@wordsHash</span><span style="color:#006600; font-weight:bold;">&#91;</span>Unscrambler::word_to_key<span style="color:#006600; font-weight:bold;">&#40;</span>word<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Loading wordlist...&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">isatty</span>
&nbsp;
u = Unscrambler.<span style="color:#9900CC;">new</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Ready.&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">isatty</span>
&nbsp;
<span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">each_line</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
    line.<span style="color:#CC0066; font-weight:bold;">gsub!</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\w<span style="color:#006600; font-weight:bold;">+/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>match<span style="color:#006600; font-weight:bold;">|</span>
        choices = u.<span style="color:#9900CC;">unscramble</span><span style="color:#006600; font-weight:bold;">&#40;</span>match.<span style="color:#9900CC;">downcase</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">case</span> choices.<span style="color:#9900CC;">size</span>
            <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">then</span> match
            <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">then</span> choices<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
            <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#996600;">&quot;[&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> choices.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;, &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;]&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<h3 id="toc-ruby-converter-for-xml-quiz-files-to-quizmaker-program"> Ruby converter for xml quiz files to Quizmaker Program</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> ‘ostruct’
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;rexml/document&quot;</span>
<span style="color:#9966CC; font-weight:bold;">include</span> REXML
<span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;What is the name of the xml file?&quot;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> myFile = <span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>
doc = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{myFile}.xml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>io<span style="color:#006600; font-weight:bold;">|</span> Document.<span style="color:#9900CC;">new</span> io<span style="color:#006600; font-weight:bold;">&#125;</span>
quiz=<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
doc.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;QUIZ/QUESTION&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>el_q<span style="color:#006600; font-weight:bold;">|</span>
  question = OpenStruct.<span style="color:#9900CC;">new</span>
  quiz.<span style="color:#9900CC;">push</span> question.<span style="color:#9900CC;">text</span> = el_q.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;TEXT&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  question.<span style="color:#9900CC;">explain</span> = el_q.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;EXPLAIN&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  question.<span style="color:#9900CC;">answer</span> = el_q.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;ANSWER&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
 i=<span style="color:#006666;">0</span>
  el_q.<span style="color:#9900CC;">elements</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;CHOICE&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>el_ch<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>question.<span style="color:#9900CC;">choices</span><span style="color:#006600; font-weight:bold;">||</span>=<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> el_ch.<span style="color:#9900CC;">text</span>
    quiz.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>question.<span style="color:#9900CC;">choices</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>i.<span style="color:#9900CC;">to_s</span>==question.<span style="color:#9900CC;">answer</span><span style="color:#006600; font-weight:bold;">&#41;</span>
     quiz.<span style="color:#9900CC;">push</span> <span style="color:#996600;">&quot;Correct! #{question.explain}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
    quiz.<span style="color:#9900CC;">push</span> <span style="color:#996600;">&quot;False! #{question.explain}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    i<span style="color:#006600; font-weight:bold;">+</span>=<span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>::<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{myFile}&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> ‘.<span style="color:#9900CC;">quiz</span>’, ‘w’<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  f.<span style="color:#CC0066; font-weight:bold;">puts</span> quiz
 <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<h3 id="toc-create-a-histogram-in-ruby">Create a Histogram in Ruby</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">counts = my_array.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>counts, key<span style="color:#006600; font-weight:bold;">|</span> counts<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>;
counts <span style="color:#006600; font-weight:bold;">&#125;</span>
counts = counts.<span style="color:#9900CC;">sort</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span> a<span style="color:#006600; font-weight:bold;">&lt;=&gt;</span>b<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># Here is an example of the technique:</span>
a=<span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>a b c d e f f f g h j j j <span style="color:#006600; font-weight:bold;">&#41;</span>
bob = a.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>counts, key<span style="color:#006600; font-weight:bold;">|</span> counts<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>;
counts <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> bob</pre></td></tr></table></div>

<h3 id="toc-clean-out-old-files-with-ruby">Clean out old files with Ruby</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
action = ‘ARCHIVE’ <span style="color:#008000; font-style:italic;"># ‘LIST’ or ‘DELETE’ or ‘TRASH’ or ‘ARCHIVE’</span>
age = <span style="color:#006666;">45</span> <span style="color:#008000; font-style:italic;"># in days (2 weeks default)</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># DO NOT CHANGE ANYTHING BELOW THIS LINE</span>
<span style="color:#008000; font-style:italic;"># Save the file somewhere as clean.rb, and in the Terminal (Applications &gt; Utilities), cd to the directory (e.g., cd Desktop if it’s on your Desktop), and type chmod +x clean.rb.</span>
<span style="color:#008000; font-style:italic;">#If you want, you can edit the top of the file.</span>
<span style="color:#008000; font-style:italic;">#There are two settings: what you want to do with the file (list, move to trash, or delete), and how long it must be since you accessed it.</span>
<span style="color:#008000; font-style:italic;">#You can then run the file by typing ./clean.rb Downloads, for example. It will clean out or list the files according to your preference.</span>
<span style="color:#008000; font-style:italic;">#It will only remove whole directories.</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> ‘fileutils’
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> distance_of_time_in_words<span style="color:#006600; font-weight:bold;">&#40;</span>from_time, to_time = <span style="color:#006666;">0</span>, include_seconds = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  from_time = from_time.<span style="color:#9900CC;">to_time</span> <span style="color:#9966CC; font-weight:bold;">if</span> from_time.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:to_time</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  to_time = to_time.<span style="color:#9900CC;">to_time</span> <span style="color:#9966CC; font-weight:bold;">if</span> to_time.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:to_time</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  distance_in_minutes = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>to_time <span style="color:#006600; font-weight:bold;">-</span> from_time<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">abs</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">60</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  distance_in_seconds = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>to_time <span style="color:#006600; font-weight:bold;">-</span> from_time<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">abs</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> distance_in_minutes
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">0</span>..<span style="color:#006666;">1</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">&#40;</span>distance_in_minutes==<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? ‘less than a minute’ : ‘<span style="color:#006666;">1</span> minute’ <span style="color:#9966CC; font-weight:bold;">unless</span> include_seconds
    <span style="color:#9966CC; font-weight:bold;">case</span> distance_in_seconds
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">0</span>..<span style="color:#006666;">5</span>   <span style="color:#9966CC; font-weight:bold;">then</span> ‘less than <span style="color:#006666;">5</span> seconds’
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">6</span>..<span style="color:#006666;">10</span>  <span style="color:#9966CC; font-weight:bold;">then</span> ‘less than <span style="color:#006666;">10</span> seconds’
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">11</span>..<span style="color:#006666;">20</span> <span style="color:#9966CC; font-weight:bold;">then</span> ‘less than <span style="color:#006666;">20</span> seconds’
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">21</span>..<span style="color:#006666;">40</span> <span style="color:#9966CC; font-weight:bold;">then</span> ‘half a minute’
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">41</span>..<span style="color:#006666;">59</span> <span style="color:#9966CC; font-weight:bold;">then</span> ‘less than a minute’
    <span style="color:#9966CC; font-weight:bold;">else</span>             ‘<span style="color:#006666;">1</span> minute’
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">2</span>..<span style="color:#006666;">45</span>      <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;#{distance_in_minutes} minutes&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">46</span>..<span style="color:#006666;">90</span>     <span style="color:#9966CC; font-weight:bold;">then</span> ‘about <span style="color:#006666;">1</span> hour’
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">90</span>..<span style="color:#006666;">1440</span>   <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;about #{(distance_in_minutes.to_f / 60.0).round} hours&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1441</span>..<span style="color:#006666;">2880</span> <span style="color:#9966CC; font-weight:bold;">then</span> ‘<span style="color:#006666;">1</span> day’
  <span style="color:#9966CC; font-weight:bold;">else</span>                 <span style="color:#996600;">&quot;#{(distance_in_minutes / 1440).round} days&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> last_accessed<span style="color:#006600; font-weight:bold;">&#40;</span>dir_name<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># puts ‘called’</span>
  best = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">chdir</span><span style="color:#006600; font-weight:bold;">&#40;</span>dir_name<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘.’<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>entry<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> entry == ‘.’ <span style="color:#9966CC; font-weight:bold;">or</span> entry == ‘..’
      <span style="color:#008000; font-style:italic;"># puts entry+’!’</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">directory</span>? entry
        x = last_accessed entry
        best = x <span style="color:#9966CC; font-weight:bold;">if</span> x <span style="color:#006600; font-weight:bold;">&gt;</span> best
      <span style="color:#9966CC; font-weight:bold;">else</span>
        x = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">atime</span><span style="color:#006600; font-weight:bold;">&#40;</span>entry<span style="color:#006600; font-weight:bold;">&#41;</span>
        best = x <span style="color:#9966CC; font-weight:bold;">if</span> x <span style="color:#006600; font-weight:bold;">&gt;</span> best
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> best
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">chdir</span><span style="color:#006600; font-weight:bold;">&#40;</span>$<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">pwd</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘.’<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>entry<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#008000; font-style:italic;"># puts entry</span>
    <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> entry == ‘.’ <span style="color:#9966CC; font-weight:bold;">or</span> entry == ‘..’
    x = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">directory</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>entry<span style="color:#006600; font-weight:bold;">&#41;</span> ? last_accessed<span style="color:#006600; font-weight:bold;">&#40;</span>entry<span style="color:#006600; font-weight:bold;">&#41;</span> : <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">atime</span><span style="color:#006600; font-weight:bold;">&#40;</span>entry<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> x <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">60</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">60</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">24</span> <span style="color:#006600; font-weight:bold;">*</span> age
      <span style="color:#9966CC; font-weight:bold;">case</span> action
      <span style="color:#9966CC; font-weight:bold;">when</span> ‘DELETE’
        <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>entry<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> ‘TRASH’
        <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">mv</span><span style="color:#006600; font-weight:bold;">&#40;</span>entry, <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘~<span style="color:#006600; font-weight:bold;">/</span>Trash<span style="color:#006600; font-weight:bold;">/</span>’<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> ‘ARCHIVE’
          <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">mv</span><span style="color:#006600; font-weight:bold;">&#40;</span>entry, <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘<span style="color:#006600; font-weight:bold;">/</span>Users<span style="color:#006600; font-weight:bold;">/</span>charleslsnyder<span style="color:#006600; font-weight:bold;">/</span>Archives’<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span> entry <span style="color:#006600; font-weight:bold;">+</span> ‘: ‘ <span style="color:#006600; font-weight:bold;">+</span> distance_of_time_in_words<span style="color:#006600; font-weight:bold;">&#40;</span>x, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2008/05/23/ruby-tag-cloud-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Web Search</title>
		<link>http://www.clsnyder.com/WordPress/2007/09/11/ruby-web-search/</link>
		<comments>http://www.clsnyder.com/WordPress/2007/09/11/ruby-web-search/#comments</comments>
		<pubDate>Tue, 11 Sep 2007 23:23:38 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=68</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 require ‘net/http’ require ‘rexml/document’ # Web search for &#34;finance&#34; url = ‘http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=cls31415o&#38;query=finance&#38;results=40' # get the XML data as a string &#8230; <a href="http://www.clsnyder.com/WordPress/2007/09/11/ruby-web-search/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> ‘net<span style="color:#006600; font-weight:bold;">/</span>http’
<span style="color:#CC0066; font-weight:bold;">require</span> ‘rexml<span style="color:#006600; font-weight:bold;">/</span>document’
<span style="color:#008000; font-style:italic;"># Web search for &quot;finance&quot;</span>
url = ‘http:<span style="color:#006600; font-weight:bold;">//</span>api.<span style="color:#9900CC;">search</span>.<span style="color:#9900CC;">yahoo</span>.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>WebSearchService<span style="color:#006600; font-weight:bold;">/</span>V1<span style="color:#006600; font-weight:bold;">/</span>webSearch?appid=cls31415o<span style="color:#006600; font-weight:bold;">&amp;</span>query=finance<span style="color:#006600; font-weight:bold;">&amp;</span>results=<span style="color:#006666;">40</span><span style="color:#996600;">'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body
# extract event information
doc = REXML::Document.new(xml_data)
titles = []
links = []
doc.elements.each(‘ResultSet/Result/Title’) do |ele|
   titles &lt;&lt; ele.text
end
doc.elements.each(‘ResultSet/Result/Url’) do |ele|
   links &lt;&lt; ele.text
end
# print all events
titles.each_with_index do |title, idx|
   print &quot;#{title} =&gt; #{links[idx]}<span style="color:#000099;">\n</span>&quot;
end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2007/09/11/ruby-web-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby interval between dates</title>
		<link>http://www.clsnyder.com/WordPress/2007/09/11/ruby-interval-between-dates/</link>
		<comments>http://www.clsnyder.com/WordPress/2007/09/11/ruby-interval-between-dates/#comments</comments>
		<pubDate>Tue, 11 Sep 2007 23:22:48 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=67</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Date &#160; def distance_to&#40;end_date&#41; years = end_date.year - year months = end_date.month - month days = end_date.day - day &#8230; <a href="http://www.clsnyder.com/WordPress/2007/09/11/ruby-interval-between-dates/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> distance_to<span style="color:#006600; font-weight:bold;">&#40;</span>end_date<span style="color:#006600; font-weight:bold;">&#41;</span>
  years = end_date.<span style="color:#9900CC;">year</span> <span style="color:#006600; font-weight:bold;">-</span> year
  months = end_date.<span style="color:#9900CC;">month</span> <span style="color:#006600; font-weight:bold;">-</span> month
  days = end_date.<span style="color:#9900CC;">day</span> <span style="color:#006600; font-weight:bold;">-</span> day
    <span style="color:#9966CC; font-weight:bold;">if</span> days <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">0</span>
  days <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">30</span>
   months <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
; <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> months <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">0</span>
;   months <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">12</span>
   years <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
 <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#006600; font-weight:bold;">&#123;</span>:years <span style="color:#006600; font-weight:bold;">=&gt;</span> years, <span style="color:#ff3333; font-weight:bold;">:months</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> months, <span style="color:#ff3333; font-weight:bold;">:days</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> days<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
c = <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span>
c = distance_to<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> c</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2007/09/11/ruby-interval-between-dates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Print hash values</title>
		<link>http://www.clsnyder.com/WordPress/2007/09/11/ruby-print-hash-values/</link>
		<comments>http://www.clsnyder.com/WordPress/2007/09/11/ruby-print-hash-values/#comments</comments>
		<pubDate>Tue, 11 Sep 2007 23:17:56 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=66</guid>
		<description><![CDATA[1 2 3 4 5 6 7 h=&#123;1=&#62;&#34;bob&#34;, 2=&#62;&#34;joe&#34;, 3=&#62;&#34;herb&#34;&#125; # m = h.keys n = h.values n.each do &#124;n&#124; puts n puts end]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">h=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;bob&quot;</span>, <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;joe&quot;</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;herb&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># m = h.keys</span>
n = h.<span style="color:#9900CC;">values</span>
n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span>
<span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> n
<span style="color:#CC0066; font-weight:bold;">puts</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2007/09/11/ruby-print-hash-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby CSV sum by column</title>
		<link>http://www.clsnyder.com/WordPress/2007/09/11/ruby-csv-sum-by-column/</link>
		<comments>http://www.clsnyder.com/WordPress/2007/09/11/ruby-csv-sum-by-column/#comments</comments>
		<pubDate>Tue, 11 Sep 2007 23:09:32 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=63</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 11 # This takes csv data, and identifies it by column header (bob), sums the x column, and then adds a column with x/sum x &#160; require ‘rubygems’ require ‘fastercsv’ &#8230; <a href="http://www.clsnyder.com/WordPress/2007/09/11/ruby-csv-sum-by-column/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># This takes csv data, and identifies it by column header (bob), sums the x column, and then adds a column with x/sum x</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> ‘rubygems’
<span style="color:#CC0066; font-weight:bold;">require</span> ‘fastercsv’
csv=<span style="color:#996600;">&quot;f,bob<span style="color:#000099;">\n</span>a,0<span style="color:#000099;">\n</span>b,1<span style="color:#000099;">\n</span>c,2<span style="color:#000099;">\n</span>d,3<span style="color:#000099;">\n</span>&quot;</span>
table = FCSV.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>csv, <span style="color:#ff3333; font-weight:bold;">:headers</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:converters</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> :<span style="color:#CC0066; font-weight:bold;">integer</span><span style="color:#006600; font-weight:bold;">&#41;</span>
xs = table<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bob'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#125;</span>
sum  = <span style="color:#CC0066; font-weight:bold;">Float</span> table<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bob'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>sum,i<span style="color:#006600; font-weight:bold;">|</span> sum <span style="color:#006600; font-weight:bold;">+</span>= i<span style="color:#006600; font-weight:bold;">&#125;</span>
norm = xs.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x <span style="color:#006600; font-weight:bold;">/</span> sum<span style="color:#006600; font-weight:bold;">&#125;</span>
table<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'n'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = norm
<span style="color:#CC0066; font-weight:bold;">puts</span> table</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2007/09/11/ruby-csv-sum-by-column/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby to create TOC from HTML</title>
		<link>http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-create-toc-from-html/</link>
		<comments>http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-create-toc-from-html/#comments</comments>
		<pubDate>Tue, 11 Sep 2007 01:17:23 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=57</guid>
		<description><![CDATA[Here is the code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 &#8230; <a href="http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-create-toc-from-html/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here is the code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;hpricot&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># HTML Outliner 0.4</span>
<span style="color:#008000; font-style:italic;"># Copyright Christoffer Sawicki 2006</span>
<span style="color:#008000; font-style:italic;"># Licensed under the same terms as Ruby</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Please send bug reports and improvements to</span>
<span style="color:#008000; font-style:italic;"># christoffer.sawicki@gmail.com</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># See the test cases at the bottom for usage</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Changes</span>
<span style="color:#008000; font-style:italic;">#  * 0.4: An element can only have one id,</span>
<span style="color:#008000; font-style:italic;">#         so pass the current id to the slugifier.</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> HTMLOutliner
  <span style="color:#9966CC; font-weight:bold;">class</span> SimpleTree
    attr_accessor <span style="color:#ff3333; font-weight:bold;">:root</span>, <span style="color:#ff3333; font-weight:bold;">:subtrees</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>root, subtrees = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@root</span> = root
      <span style="color:#0066ff; font-weight:bold;">@subtrees</span> = subtrees
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> HTMLOutliner
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>hpricot_doc<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@doc</span> = hpricot_doc
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> add_header_anchors!<span style="color:#006600; font-weight:bold;">&#40;</span>slugifier = method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:default_slugifier</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    headers.<span style="color:#9900CC;">each_with_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>header, index<span style="color:#006600; font-weight:bold;">|</span>
      header<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span> = slugifier.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>header.<span style="color:#9900CC;">inner_html</span>, index, header<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> headers
    <span style="color:#0066ff; font-weight:bold;">@headers</span> <span style="color:#006600; font-weight:bold;">||</span>= find_headers
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> outline
    <span style="color:#0066ff; font-weight:bold;">@outline</span> <span style="color:#006600; font-weight:bold;">||</span>= build_outline
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  protected
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> default_slugifier<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span>, index, current_id<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># Just overwrite the current id</span>
    <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">downcase</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>s<span style="color:#006600; font-weight:bold;">+/</span>, <span style="color:#996600;">&quot;_&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;_section&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> find_headers
    find_headers_helper<span style="color:#006600; font-weight:bold;">&#40;</span>@doc.<span style="color:#9900CC;">children</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> find_headers_helper<span style="color:#006600; font-weight:bold;">&#40;</span>nodes<span style="color:#006600; font-weight:bold;">&#41;</span>
    nodes.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sum, node<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> node.<span style="color:#9900CC;">is_a</span>? <span style="color:#6666ff; font-weight:bold;">Hpricot::Text</span>
        sum
      <span style="color:#9966CC; font-weight:bold;">elsif</span> node.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^h<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>$<span style="color:#006600; font-weight:bold;">/</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>
        sum <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#91;</span> node <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> find_headers_helper<span style="color:#006600; font-weight:bold;">&#40;</span>node.<span style="color:#9900CC;">children</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        sum <span style="color:#006600; font-weight:bold;">+</span> find_headers_helper<span style="color:#006600; font-weight:bold;">&#40;</span>node.<span style="color:#9900CC;">children</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> build_outline
    <span style="color:#008000; font-style:italic;"># TODO: This code needs some lovin' :-)</span>
&nbsp;
    result = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    tree_stack = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    headers.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>header<span style="color:#006600; font-weight:bold;">|</span>
      new_tree = SimpleTree.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>header<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">until</span> tree_stack.<span style="color:#9900CC;">empty</span>? <span style="color:#006600; font-weight:bold;">||</span> tree_stack.<span style="color:#9900CC;">last</span>.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">&lt;</span> header.<span style="color:#9900CC;">name</span>
        tree_stack.<span style="color:#9900CC;">pop</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> tree_stack.<span style="color:#9900CC;">empty</span>?
        result.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>new_tree<span style="color:#006600; font-weight:bold;">&#41;</span>
        tree_stack.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>new_tree<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        tree_stack.<span style="color:#9900CC;">last</span>.<span style="color:#9900CC;">subtrees</span>.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>new_tree<span style="color:#006600; font-weight:bold;">&#41;</span>
        tree_stack.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>new_tree<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#0000FF; font-weight:bold;">return</span> result
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">__FILE__</span> == $0
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;test/unit&quot;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> HTMLOutlinerTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span></span>
    <span style="color:#9966CC; font-weight:bold;">def</span> test_construction_with_root
      assert_nothing_raised <span style="color:#9966CC; font-weight:bold;">do</span>
        HTMLOutliner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;a&gt;&lt;/a&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">headers</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_construction_without_root
      assert_nothing_raised <span style="color:#9966CC; font-weight:bold;">do</span>
        HTMLOutliner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;a&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">headers</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>      
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_headers
      input = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-hello&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Hello<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-world&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>World<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      doc = Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span>input<span style="color:#006600; font-weight:bold;">&#41;</span>
      outliner = HTMLOutliner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      assert_equal <span style="color:#006666;">2</span>,       outliner.<span style="color:#9900CC;">headers</span>.<span style="color:#9900CC;">size</span>
      assert_equal <span style="color:#996600;">&quot;Hello&quot;</span>, outliner.<span style="color:#9900CC;">headers</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inner_html</span>
      assert_equal <span style="color:#996600;">&quot;World&quot;</span>, outliner.<span style="color:#9900CC;">headers</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inner_html</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_add_anchors
      input = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-hello1&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Hello<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-world1&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>World<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      expected_output = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-hello2&quot;</span> id=<span style="color:#996600;">&quot;hello_section&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Hello<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-world2&quot;</span> id=<span style="color:#996600;">&quot;world_section&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>World<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      doc = Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span>input<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      outliner = HTMLOutliner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">&#41;</span>
      outliner.<span style="color:#9900CC;">add_header_anchors</span>!
&nbsp;
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_output, doc.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_add_anchors_with_existing_id
      input = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-hello3&quot;</span> id=<span style="color:#996600;">&quot;moo&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Hello<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      expected_output = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-hello4&quot;</span> id=<span style="color:#996600;">&quot;hello_section&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Hello<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      doc = Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span>input<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      outliner = HTMLOutliner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">&#41;</span>
      outliner.<span style="color:#9900CC;">add_header_anchors</span>!
&nbsp;
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_output, doc.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_add_anchors_with_custom_slugifier
      input = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-hello5&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Hello<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      expected_output = <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-hello6&quot;</span> id=<span style="color:#996600;">&quot;chapter1&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Hello<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      doc = Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span>input<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      outliner = HTMLOutliner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">&#41;</span>
      slugifier = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x, i, _<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;chapter#{i + 1}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      outliner.<span style="color:#9900CC;">add_header_anchors</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>slugifier<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_output, doc.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>    
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_outline
      doc = Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#006600; font-weight:bold;">&lt;</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-a&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>A<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h1 id=<span style="color:#996600;">&quot;toc-b&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>B<span style="color:#006600; font-weight:bold;">&lt;/</span>h1<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h2 id=<span style="color:#996600;">&quot;toc-c&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>C<span style="color:#006600; font-weight:bold;">&lt;/</span>h2<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h3 id=<span style="color:#996600;">&quot;toc-d&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>D<span style="color:#006600; font-weight:bold;">&lt;/</span>h3<span style="color:#006600; font-weight:bold;">&gt;</span>
          <span style="color:#006600; font-weight:bold;">&lt;</span>h2 id=<span style="color:#996600;">&quot;toc-e&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>E<span style="color:#006600; font-weight:bold;">&lt;/</span>h2<span style="color:#006600; font-weight:bold;">&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;/</span>body<span style="color:#006600; font-weight:bold;">&gt;</span>
      <span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
      outline = HTMLOutliner.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">outline</span>
&nbsp;
      assert_equal <span style="color:#006666;">2</span>, outline.<span style="color:#9900CC;">size</span>
      assert_equal <span style="color:#006666;">0</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span>.<span style="color:#9900CC;">size</span>
      assert_equal <span style="color:#006666;">2</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span>.<span style="color:#9900CC;">size</span>
      assert_equal <span style="color:#006666;">1</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span>.<span style="color:#9900CC;">size</span>
      assert_equal <span style="color:#006666;">0</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span>.<span style="color:#9900CC;">size</span>
      assert_equal <span style="color:#006666;">0</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span>.<span style="color:#9900CC;">size</span>
&nbsp;
      assert_equal <span style="color:#996600;">&quot;A&quot;</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">inner_html</span>
      assert_equal <span style="color:#996600;">&quot;B&quot;</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">inner_html</span>
      assert_equal <span style="color:#996600;">&quot;C&quot;</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">inner_html</span>
      assert_equal <span style="color:#996600;">&quot;D&quot;</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">inner_html</span>
      assert_equal <span style="color:#996600;">&quot;E&quot;</span>, outline<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">subtrees</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">inner_html</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
 <span style="color:#006600; font-weight:bold;">&amp;</span>nbs</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-create-toc-from-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby to G Map</title>
		<link>http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-g-map/</link>
		<comments>http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-g-map/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 20:37:08 +0000</pubDate>
		<dc:creator>clsnyder</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.clsnyder.com/WordPress/?p=56</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 require ‘csv’ mydata = &#91;&#93; zips = File.open&#40;‘zips.txt’&#41; do &#124;io&#124; h = &#123;&#125; io.eachline do &#124;line&#124; zip, freq = CSV.parseline line h&#91;zip&#93;=freq end h end &#8230; <a href="http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-g-map/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> ‘csv’ mydata = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> zips = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘zips.<span style="color:#9900CC;">txt</span>’<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>io<span style="color:#006600; font-weight:bold;">|</span>
h = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
io.<span style="color:#9900CC;">eachline</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
 zip, freq = CSV.<span style="color:#9900CC;">parseline</span> line
   h<span style="color:#006600; font-weight:bold;">&#91;</span>zip<span style="color:#006600; font-weight:bold;">&#93;</span>=freq
<span style="color:#9966CC; font-weight:bold;">end</span>
h
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>“target.<span style="color:#9900CC;">xml</span>”,“w”<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>target<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>‘ZIP_CODES.<span style="color:#9900CC;">txt</span>’<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>io<span style="color:#006600; font-weight:bold;">|</span>
io.<span style="color:#9900CC;">eachline</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
 rec = CSV.<span style="color:#9900CC;">parseline</span> line
 freq = zips<span style="color:#006600; font-weight:bold;">&#91;</span>rec<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">or</span> <span style="color:#9966CC; font-weight:bold;">next</span>
 rec<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>=freq
   target.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>“” <span style="color:#006600; font-weight:bold;">+</span> “n”<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.clsnyder.com/WordPress/2007/09/10/ruby-to-g-map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

