<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en" xml:base="http://www.pvle.be/wp-atom.php">
	<title type="text">Philippe</title>
	<subtitle type="text">I'm not that good but I'm not that bad</subtitle>

	<updated>2011-10-19T19:50:21Z</updated>

	<link rel="alternate" type="text/html" href="http://www.pvle.be" />
	<id>http://www.pvle.be/feed/atom/</id>
	

	<generator uri="http://wordpress.org/" version="3.3.2">WordPress</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/pvle" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="pvle" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Diablo III]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/10/diablo-iii/" />
		<id>http://www.pvle.be/2011/10/diablo-iii/</id>
		<updated>2011-10-19T19:50:21Z</updated>
		<published>2011-10-19T19:50:21Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Diablo III" />		<summary type="html"><![CDATA[I have been playing the beta for a while now and maxed out a barbarian. I have to say that it’s still Diablo, nothing changed much compared to the first two. But that’s why I like it I guess! Plus the little exclusivity of playing the closed Beta, too!]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/10/diablo-iii/"><![CDATA[<p>I have been playing the beta for a while now and maxed out a barbarian.</p>
<p><a href="http://www.pvle.be/wp-content/2011/10/Diablo002.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Diablo002" border="0" alt="Diablo002" src="http://www.pvle.be/wp-content/2011/10/Diablo002_thumb.jpg" width="244" height="154" /></a></p>
<p>I have to say that it’s still Diablo, nothing changed much compared to the first two. But that’s why I like it I guess!</p>
<p>Plus the little exclusivity of playing the closed Beta, too!</p>
]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/10/diablo-iii/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/10/diablo-iii/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Version Control by Example]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/09/version-control-by-example/" />
		<id>http://www.pvle.be/2011/09/version-control-by-example/</id>
		<updated>2011-09-27T17:39:35Z</updated>
		<published>2011-09-27T17:39:35Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" />		<summary type="html"><![CDATA[A few weeks ago, I “requested” a free copy of the book Version Control by Example by Eric Sink. Today, while coming home from work, I found a printed copy in my post box. Thanks Eric!]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/09/version-control-by-example/"><![CDATA[<p>A few weeks ago, I “requested” a free copy of the book <a href="http://www.ericsink.com/vcbe/">Version Control by Example</a> by <a href="http://www.ericsink.com/">Eric Sink</a>.</p>
<p>Today, while coming home from work, I found a printed copy in my post box.</p>
<p>Thanks Eric!</p>
]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/09/version-control-by-example/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/09/version-control-by-example/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Io, Day 3]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/io-day-3/" />
		<id>http://www.pvle.be/2011/05/io-day-3/</id>
		<updated>2011-05-29T16:37:30Z</updated>
		<published>2011-05-29T16:37:30Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Io" /><category scheme="http://www.pvle.be" term="Seven Languages in Seven Weeks" />		<summary type="html"><![CDATA[Final day with Io. It’s been quite a ride, and even if I don’t think I grasp everything that Io is, I consider playing with Io a little bit on my own later because of it’s potential, especially in DSL. Enhance the XML program to add spaces to show the indentation structure Builder := Object [...]]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/io-day-3/"><![CDATA[<p>Final day with Io. It’s been quite a ride, and even if I don’t think I grasp everything that Io is, I consider playing with Io a little bit on my own later because of it’s potential, especially in <a href="http://en.wikipedia.org/wiki/Domain-specific_language">DSL</a>.</p>
<h5>Enhance the XML program to add spaces to show the indentation structure</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">Builder := <span style="color: #000066;">Object</span> <span style="color: #000000; font-weight: bold;">clone</span>
Builder depth := <span style="color: #cc66cc;">0</span>
&nbsp;
Builder <span style="color: #000000; font-weight: bold;">forward</span> := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>
  prefix := <span style="color: #ff0000;">&quot;  &quot;</span> repeated<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span> depth<span style="color: #66cc66;">&#41;</span>
  writeln<span style="color: #66cc66;">&#40;</span>prefix, <span style="color: #ff0000;">&quot;&lt;&quot;</span>, call <span style="color: #000000; font-weight: bold;">message</span> name, <span style="color: #ff0000;">&quot;&gt;&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">self</span> depth = <span style="color: #000000; font-weight: bold;">self</span> depth + <span style="color: #cc66cc;">1</span>
  call <span style="color: #000000; font-weight: bold;">message</span> arguments <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span>arg,
    content := <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">doMessage</span><span style="color: #66cc66;">&#40;</span>arg<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>content <span style="color: #000000; font-weight: bold;">type</span> == <span style="color: #ff0000;">&quot;Sequence&quot;</span>, writeln<span style="color: #66cc66;">&#40;</span>prefix, <span style="color: #ff0000;">&quot;  &quot;</span>, content<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">self</span> depth := <span style="color: #000000; font-weight: bold;">self</span> depth - <span style="color: #cc66cc;">1</span>
  writeln<span style="color: #66cc66;">&#40;</span>prefix, <span style="color: #ff0000;">&quot;&lt;/&quot;</span>, call <span style="color: #000000; font-weight: bold;">message</span> name, <span style="color: #ff0000;">&quot;&gt;&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Builder html<span style="color: #66cc66;">&#40;</span>head<span style="color: #66cc66;">&#40;</span>title<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Programing languages&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>, body<span style="color: #66cc66;">&#40;</span>ul<span style="color: #66cc66;">&#40;</span>li<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;IO&quot;</span><span style="color: #66cc66;">&#41;</span>, li<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Lua&quot;</span><span style="color: #66cc66;">&#41;</span>, li<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;JavaScript&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h5>Create a list syntax that uses brackets</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">curlyBrackets := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>
  l := <span style="color: #000000; font-weight: bold;">List</span> <span style="color: #000000; font-weight: bold;">clone</span>
  call <span style="color: #000000; font-weight: bold;">message</span> arguments <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span>arg,
    l append<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">doMessage</span><span style="color: #66cc66;">&#40;</span>arg<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">return</span> l
<span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/io-day-3/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/io-day-3/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Io, Day 3]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/io-day-3-2/" />
		<id>http://www.pvle.be/2011/05/io-day-two/</id>
		<updated>2011-05-29T16:39:34Z</updated>
		<published>2011-05-25T19:50:00Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Io" /><category scheme="http://www.pvle.be" term="Seven Languages in Seven Weeks" />		<summary type="html"><![CDATA[Let’s get down to some coding. Write a method to find the nth Fibonacci number, both iteratively and recursively Fibonacci := Object clone &#160; Fibonacci iter := method&#40;n, l := list&#40;1, 1&#41; for&#40;i, 2, n, 1, l append&#40;l at&#40;i - 2&#41; + l at&#40;i - 1&#41;&#41; &#41; l at&#40;n - 1&#41; &#41; &#160; Fibonacci rec [...]]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/io-day-3-2/"><![CDATA[<p>Let’s get down to some coding.</p>
<h5>Write a method to find the nth <a href="http://en.wikipedia.org/wiki/Fibonacci_number">Fibonacci</a> number, both iteratively and recursively</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">Fibonacci := <span style="color: #000066;">Object</span> <span style="color: #000000; font-weight: bold;">clone</span>
&nbsp;
Fibonacci iter := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>n, 
  l := <span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">2</span>, n, <span style="color: #cc66cc;">1</span>, 
    l append<span style="color: #66cc66;">&#40;</span>l at<span style="color: #66cc66;">&#40;</span>i - <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> + l at<span style="color: #66cc66;">&#40;</span>i - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  l at<span style="color: #66cc66;">&#40;</span>n - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Fibonacci rec := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>n,
  <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>n <span style="color: #66cc66;">&amp;</span>lt; <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span>, 
    rec<span style="color: #66cc66;">&#40;</span>n - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + rec<span style="color: #66cc66;">&#40;</span>n - <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
x := Fibonacci iter<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
x println
&nbsp;
y := Fibonacci rec<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
y println</pre></div></div>

<h5>How would you change the operator / to return 0 is the denominator is zero</h5>
<p>For this one, I was a bit stuck with only the book and the language reference, so I had to <a href="http://www.google.be/search?q=io+language+recursive+method">Google</a> it. And that’s where you have all the beauty and annoyance of the internet: one of the first hit is a solution to that exact question on <a href="http://stackoverflow.com/questions/4262196/how-do-you-replace-existing-operators-without-invoking-them-in-io">StackOverflow</a> (it was to be expected.) I was expecting this exercise to take me to the <em>OperatorTable</em> for which there is an example in the book, but actually the operator already exists so it does not have to be added, but instead re-defined for the Number type.</p>
<h5>Write a program to add up all the numbers in a two-dimensional array</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">a := <span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
a append<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
a append<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">11</span>, <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
a append<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span>, <span style="color: #cc66cc;">14</span>, <span style="color: #cc66cc;">15</span>, <span style="color: #cc66cc;">16</span>, <span style="color: #cc66cc;">17</span>, <span style="color: #cc66cc;">18</span>, <span style="color: #cc66cc;">19</span>, <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
total := <span style="color: #cc66cc;">0</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, a size - <span style="color: #cc66cc;">1</span>,
  b := a at<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j, <span style="color: #cc66cc;">0</span>, b size - <span style="color: #cc66cc;">1</span>,
    n := b at<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span>
    total = <span style="color: #66cc66;">&#40;</span>total + n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
total println</pre></div></div>

<h5>Add an <em>myAverage</em> slot to a list that computes the average of all the numbers&nbsp; in a list. What happens if there are no numbers in a list? Bonus: raise an exception if any item in the list is not a <em>Number</em>.</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">a := <span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
b := <span style="color: #000000; font-weight: bold;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #ff0000;">&quot;hello&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">List</span> myAverage := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>
  total := <span style="color: #cc66cc;">0</span>
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">self</span> size -<span style="color: #cc66cc;">1</span>,
    n := <span style="color: #000000; font-weight: bold;">self</span> at<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>n <span style="color: #000000; font-weight: bold;">type</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">&quot;Number&quot;</span>,
      <span style="color: #000066;">Exception</span> <span style="color: #b1b100;">raise</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;An item in the list is not a Number&quot;</span><span style="color: #66cc66;">&#41;</span>,
      total = total + n
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  total <span style="color: #66cc66;">/</span> <span style="color: #000000; font-weight: bold;">self</span> size
<span style="color: #66cc66;">&#41;</span>
&nbsp;
a myAverage println
b myAverage println</pre></div></div>

<h5>Write a prototype for a two-dimensional list. The <em>dim(x, y)</em> method should allocate a list of y lists that are x elements long. The <em>set(x, y, value)</em> method should set a value and the <em>get(x, y)</em> method should return that value.</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">Matrix := <span style="color: #000066;">Object</span> <span style="color: #000000; font-weight: bold;">clone</span>
&nbsp;
Matrix <span style="color: #000000; font-weight: bold;">list</span> := <span style="color: #000066;">nil</span>
&nbsp;
Matrix dim := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>x, y,
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> := <span style="color: #000000; font-weight: bold;">List</span> <span style="color: #000000; font-weight: bold;">clone</span>
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, x - <span style="color: #cc66cc;">1</span>,
    l := <span style="color: #000000; font-weight: bold;">List</span> <span style="color: #000000; font-weight: bold;">clone</span>
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j, <span style="color: #cc66cc;">0</span>, y - <span style="color: #cc66cc;">1</span>, l append<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> append<span style="color: #66cc66;">&#40;</span>l<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix set := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>x, y, value,
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> at<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> atPut<span style="color: #66cc66;">&#40;</span>y, value<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix get := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>x, y,
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> at<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> at<span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
matrix := Matrix <span style="color: #000000; font-weight: bold;">clone</span>
matrix dim<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
matrix set<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #ff0000;">&quot;Hello&quot;</span><span style="color: #66cc66;">&#41;</span>
matrix set<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #ff0000;">&quot;World&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
matrix get<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> println
matrix get<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> println</pre></div></div>

<h5>Bonnus: write a transpose method so that new_matrix get(y, x) == matrix get(x, y) on the original list</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">Matrix transpose := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>
  x := <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> at<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> size
  y := <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> size
  m := Matrix <span style="color: #000000; font-weight: bold;">clone</span>
  m dim<span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, x - <span style="color: #cc66cc;">1</span>,
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j, <span style="color: #cc66cc;">0</span>, y - <span style="color: #cc66cc;">1</span>,
      m set<span style="color: #66cc66;">&#40;</span>i, j, <span style="color: #000000; font-weight: bold;">self</span> get<span style="color: #66cc66;">&#40;</span>j, i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  m
<span style="color: #66cc66;">&#41;</span>
&nbsp;
new_matrix := matrix transpose
&nbsp;
<span style="color: #66cc66;">&#40;</span>matrix get<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> == new_matrix get<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> println</pre></div></div>

<h5>Write the matrix to a file, read the matrix from a file.</h5>
<pre code="IO"></pre>
<p>For this one, decided to aggressively look in the List API to make the code simpler. I discovered quite a lot of useful functions! I also implemented the <em>asString</em> method so that it is easier to debug (the <em>println</em> method on the Object type is implemented to print the result of <em>asString</em>.)</p>
<p>Here is the full code for the whole Matrix object, improved as much as I could:</p>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;">Matrix := <span style="color: #000066;">Object</span> <span style="color: #000000; font-weight: bold;">clone</span>
&nbsp;
Matrix <span style="color: #000000; font-weight: bold;">list</span> := <span style="color: #000066;">nil</span>
&nbsp;
Matrix dim := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>x, y,
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> := <span style="color: #000000; font-weight: bold;">List</span> <span style="color: #000000; font-weight: bold;">clone</span> setSize<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, x - <span style="color: #cc66cc;">1</span>,
    <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> atPut<span style="color: #66cc66;">&#40;</span>i, <span style="color: #000000; font-weight: bold;">List</span> <span style="color: #000000; font-weight: bold;">clone</span> setSize<span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix set := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>x, y, value,
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> at<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> atPut<span style="color: #66cc66;">&#40;</span>y, value<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix get := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>x, y,
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> at<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> at<span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix transpose := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>
  x := <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> at<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> size
  y := <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> size
  m := Matrix <span style="color: #000000; font-weight: bold;">clone</span>
  m dim<span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, x - <span style="color: #cc66cc;">1</span>,
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j, <span style="color: #cc66cc;">0</span>, y - <span style="color: #cc66cc;">1</span>,
      m set<span style="color: #66cc66;">&#40;</span>i, j, <span style="color: #000000; font-weight: bold;">self</span> get<span style="color: #66cc66;">&#40;</span>j, i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">return</span> m
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix <span style="color: #000000; font-weight: bold;">asString</span> := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span>
  s := <span style="color: #ff0000;">&quot;&quot;</span>
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span>e,
    s := s asMutable appendSeq<span style="color: #66cc66;">&#40;</span>e join<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> asMutable appendSeq<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix saveTo := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file</span>,
  f := <span style="color: #000066;">File</span> with<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file</span><span style="color: #66cc66;">&#41;</span> remove openForUpdating
  <span style="color: #000000; font-weight: bold;">self</span> <span style="color: #000000; font-weight: bold;">list</span> <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span>i, e,
    f <span style="color: #000000; font-weight: bold;">write</span><span style="color: #66cc66;">&#40;</span>e join<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">write</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  f close
<span style="color: #66cc66;">&#41;</span>
&nbsp;
Matrix loadFrom := <span style="color: #000000; font-weight: bold;">method</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file</span>,
  l := <span style="color: #000000; font-weight: bold;">List</span> <span style="color: #000000; font-weight: bold;">clone</span>
  f := <span style="color: #000066;">File</span> with<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file</span><span style="color: #66cc66;">&#41;</span> openForReading
  f readLines <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span>e,
    l append<span style="color: #66cc66;">&#40;</span>e splitNoEmpties<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  f close
  m := Matrix <span style="color: #000000; font-weight: bold;">clone</span>
  m dim<span style="color: #66cc66;">&#40;</span>l size, l at<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> size<span style="color: #66cc66;">&#41;</span>
  l <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span>i, e,
    e <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span>j, f,
      m set<span style="color: #66cc66;">&#40;</span>i, j, f<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  m
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Tests */</span>
matrix saveTo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;matrix.txt&quot;</span><span style="color: #66cc66;">&#41;</span>
new_matrix saveTo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;new_matrix.txt&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
matrix := Matrix loadFrom<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;matrix.txt&quot;</span><span style="color: #66cc66;">&#41;</span>
new_matrix := Matrix loadFrom<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;new_matrix.txt&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
matrix println
new_matrix println</pre></div></div>

<h5>Write a program that gives you 10 tries to guess a random number from 1 to 100. Give hints of “Hotter” or “Colder” if you like.</h5>

<div class="wp_syntax"><div class="code"><pre class="io" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Not really random, but will suffice for now</span>
n := <span style="color: #000066;">Date</span> now second <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">1000</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">100</span>
&nbsp;
input := <span style="color: #000066;">File</span> standardInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">10</span>,
  guess := input readLine asNumber
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>guess <span style="color: #66cc66;">&amp;</span>lt; n, <span style="color: #ff0000;">&quot;Hotter&quot;</span> println<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>guess <span style="color: #66cc66;">&amp;</span>gt; n, <span style="color: #ff0000;">&quot;Colder&quot;</span> println<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>guess == n,
    <span style="color: #ff0000;">&quot;Well done!&quot;</span> println
    <span style="color: #b1b100;">break</span>
  <span style="color: #66cc66;">&#41;</span>  
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I really enjoyed myself with these! Looking forward to Day 3&#8230;</p>
]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/io-day-3-2/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/io-day-3-2/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Io, Day 1]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/io-day-1/" />
		<id>http://www.pvle.be/2011/05/io-day-one/</id>
		<updated>2011-05-29T16:39:14Z</updated>
		<published>2011-05-19T20:08:00Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Io" /><category scheme="http://www.pvle.be" term="Seven Languages in Seven Weeks" />		<summary type="html"><![CDATA[First day on Io, which I never heard about… Io is a dynamic prototype-based programming language. Apart from JavaScript, I never heard any other prototype-based language. And even in JavaScript, I don’t know exactly what this implies, so my expectation of learning new things while studying Io is quite high, which is good! By the [...]]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/io-day-1/"><![CDATA[<p>First day on Io, which I never heard about…</p>
<blockquote><p>Io is a dynamic prototype-based programming language.</p>
</blockquote>
<p>Apart from JavaScript, I never heard any other prototype-based language. And even in JavaScript, I don’t know exactly what this implies, so my expectation of learning new things while studying Io is quite high, which is good!</p>
<p>By the way, Io is the name of a <a href="http://en.wikipedia.org/wiki/Io_(mythology)">nymph in Greek mythology</a> and <a href="http://en.wikipedia.org/wiki/Io_(moon)">one of Jupiter’s satellite</a>. Believe it or not, but I knew that before encountering the language, and still wonder if there is any relation whatsoever, but didn’t find anything about the language name’s origin…</p>
<p><a href="http://www.pvle.be/wp-content/2011/05/Io.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Io" border="0" alt="Io" src="http://www.pvle.be/wp-content/2011/05/Io_thumb.png" width="105" height="244" /></a></p>
<p>(to me it was the perfect excuse to put a picture of a sexy girl on my blog and get more traffic!)</p>
<p>Anyway, first task, get Io to run on my box, namely a Windows 7 x64 machine. After <a href="http://www.iolanguage.com/">downloading the sources</a> and trying to build them according to the readme.txt, lots of errors…</p>
<p>Finally, I found that the best solution is to <a href="http://www.iolanguage.com/downloads/">download the binaries</a> which work fine on my machine. That’ll do for now.</p>
<p>I also needed some reference to lookup to when in doubt, and that’s all available <a href="http://iolanguage.com/scm/io/docs/IoGuide.html">here</a>.</p>
<p>So, on day one, no real assignments, or at least nothing that can’t be found in 30 seconds on the official website, so I’m not going to replicate this here.</p>
]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/io-day-1/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/io-day-1/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Ruby, Day 3]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/ruby-day-3/" />
		<id>http://www.pvle.be/2011/05/ruby-day-3/</id>
		<updated>2011-05-17T15:02:00Z</updated>
		<published>2011-05-17T15:02:00Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Ruby" /><category scheme="http://www.pvle.be" term="Seven Languages in Seven Weeks" />		<summary type="html"><![CDATA[Last day on Ruby! Retrospectively, I didn’t knew Ruby before (and I still don’t know much about it, admittedly), but it’s been fun. I’m not a big fan after what I saw. I still find it a bit messy, but that’s maybe because I’m not used to it enough. The “you can rewrite anything and [...]]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/ruby-day-3/"><![CDATA[<p>Last day on Ruby!</p>
<p>Retrospectively, I didn’t knew Ruby before (and I still don’t know much about it, admittedly), but it’s been fun.</p>
<p>I’m not a big fan after what I saw. I still find it a bit messy, but that’s maybe because I’m not used to it enough. The “you can rewrite anything and change basic behavior” is very nice, but I seriously wonder how many times it has bitten back developers. If someone rewrites one of the basic methods in a big codebase, I can only imagine the consequences, if you are luck enough to see them right away…</p>
<p>However, I enjoyed scripting with Ruby very much, and it was a good brainteaser to try to find the effective way of doing those things in a language I completely ignored so far.</p>
<p>Anyway, here is my last homework!</p>
<h2>Do</h2>
<p>So, the goal here is to modify a class that reads CSV files and add an <em>each</em> method that will perform a block on a CsvRow object, object that is to be written. This object should have its method_missing overwritten so that you can access columns as if they were methods:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActsAsCsv
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">extend</span> ClassMethods
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> acts_as_csv
      <span style="color:#9966CC; font-weight:bold;">include</span> InstanceMethods
    <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;">module</span> InstanceMethods
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> read
      <span style="color:#0066ff; font-weight:bold;">@csv_contents</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      filename = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">downcase</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.csv'</span>
      file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@headers</span> = file.<span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">', '</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      file.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#0066ff; font-weight:bold;">@csv_contents</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> row.<span style="color:#CC0066; font-weight:bold;">chomp</span>.<span style="color:#CC0066; font-weight:bold;">split</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;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    attr_accessor <span style="color:#ff3333; font-weight:bold;">:headers</span>, <span style="color:#ff3333; font-weight:bold;">:csv_contents</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize
      read 
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> each<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@csv_contents</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span> block.<span style="color:#9900CC;">call</span> CsvRow.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>@headers, line<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> RubyCsv  <span style="color:#008000; font-style:italic;"># no inheritance! You can mix it in</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> ActsAsCsv
  acts_as_csv
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CsvRow
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:headings</span>, <span style="color:#ff3333; font-weight:bold;">:values</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>headings, values<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@headings</span> = headings
    <span style="color:#0066ff; font-weight:bold;">@values</span> = <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span>headings.<span style="color:#9900CC;">zip</span><span style="color:#006600; font-weight:bold;">&#40;</span>values<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>e<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, e<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:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</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;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>name, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    col = name.<span style="color:#9900CC;">to_s</span>
    <span style="color:#0066ff; font-weight:bold;">@values</span><span style="color:#006600; font-weight:bold;">&#91;</span>col<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
csv = RubyCsv.<span style="color:#9900CC;">new</span>
csv.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> row.<span style="color:#9900CC;">two</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/ruby-day-3/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/ruby-day-3/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Ruby, Day 2]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/ruby-day-2/" />
		<id>http://www.pvle.be/2011/05/ruby-day-2/</id>
		<updated>2011-05-15T13:13:00Z</updated>
		<published>2011-05-15T13:13:00Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Ruby" /><category scheme="http://www.pvle.be" term="Seven Languages in Seven Weeks" />		<summary type="html"><![CDATA[Find #File IO without block #f = File.new(&#34;testfile&#34;, &#34;r&#34;) #f.close &#160; #File IO with block, implicit call to f.close #File.open(&#34;testfile&#34;, &#34;r&#34;) do &#124;aFile&#124; end &#160; #Hash to array h = &#123; 1 =&#62; 2, &#34;cat&#34; =&#62; &#34;tom&#34; &#125; a = h.to_a &#160; #Array to hash a = &#40;1..10&#41;.to_a h = Hash&#91;a.map &#123; &#124;i&#124; &#91;i, i [...]]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/ruby-day-2/"><![CDATA[<h2>Find</h2>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#File IO without block</span>
<span style="color:#008000; font-style:italic;">#f = File.new(&quot;testfile&quot;, &quot;r&quot;)</span>
<span style="color:#008000; font-style:italic;">#f.close</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#File IO with block, implicit call to f.close</span>
<span style="color:#008000; font-style:italic;">#File.open(&quot;testfile&quot;, &quot;r&quot;) do |aFile| end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Hash to array</span>
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:#006666;">2</span>, <span style="color:#996600;">&quot;cat&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;tom&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
a = h.<span style="color:#9900CC;">to_a</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Array to hash</span>
a = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>
h = <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span>a.<span style="color:#9900CC;">map</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> <span style="color:#006600; font-weight:bold;">&#91;</span>i, i <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:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Iterate through a hash</span>
h.<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> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{i[0]} : #{i[1]}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Use array as stack</span>
a.<span style="color:#9900CC;">push</span> <span style="color:#006666;">19</span>
a.<span style="color:#9900CC;">pop</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Arrays can be used as: stacks, sets, queues, dequeues, and fifos</span></pre></div></div>

<h2>Do</h2>
<p>Print the content of an array of sixteen numbers, four numbers at a time, using just each. Now, do the same with each_slice in Enumerable:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#Using each (interesting fact: there is a closure on a here)</span>
a.<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> <span style="color:#9966CC; font-weight:bold;">if</span> i <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">4</span> == <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{a[i-4..i-1]}&quot;</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Using each_slice </span>
a.<span style="color:#9900CC;">each_slice</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</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>s<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{s}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Rewrite the Tree class so it can be initialized with a nested structure with hashes and arrays:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#Initial code from the book</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Tree
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:children</span>, <span style="color:#ff3333; font-weight:bold;">:node_name</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>name, children=<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;">@children</span> = children
    <span style="color:#0066ff; font-weight:bold;">@node_name</span> = name
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> visit_all<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    visit <span style="color:#006600; font-weight:bold;">&amp;</span>block
    <span style="color:#0066ff; font-weight:bold;">@children</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> c.<span style="color:#9900CC;">visit_all</span> <span style="color:#006600; font-weight:bold;">&amp;</span>block <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> visit<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    block.<span style="color:#9900CC;">call</span> <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
t = Tree.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Ruby&quot;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span>Tree.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Gem&quot;</span><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;">&#41;</span>
t.<span style="color:#9900CC;">visit_all</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{t.node_name}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Starting from that, this is what we want to be able to write:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Tree.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'grandpa'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'dad'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'child 1'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'child 2'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <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;">&#125;</span>, <span style="color:#996600;">'uncle'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'child 3'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'child 4'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <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;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>And here is my solution to it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Tree
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:children</span>, <span style="color:#ff3333; font-weight:bold;">:node_name</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>structure<span style="color:#006600; font-weight:bold;">&#41;</span>
    structure.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#0066ff; font-weight:bold;">@node_name</span> = key
      <span style="color:#0066ff; font-weight:bold;">@children</span> = value.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> Tree.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span> e<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;">=&gt;</span> e<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:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <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>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> visit_all<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    visit <span style="color:#006600; font-weight:bold;">&amp;</span>block
    <span style="color:#0066ff; font-weight:bold;">@children</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> c.<span style="color:#9900CC;">visit_all</span> <span style="color:#006600; font-weight:bold;">&amp;</span>block <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> visit<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    block.<span style="color:#9900CC;">call</span> <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
t = Tree.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'grandpa'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'dad'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'child 1'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'child 2'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <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;">&#125;</span>, <span style="color:#996600;">'uncle'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'child 3'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'child 4'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <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;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
t.<span style="color:#9900CC;">visit_all</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{t.node_name}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Write a simple grep that will print the lines of a file having any occurrences of a phrase anywhere in that line. Use Regular Expressions and include line numbers:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">regex = <span style="color:#CC00FF; font-weight:bold;">Regexp</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'sit amet'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
ln = <span style="color:#006666;">0</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">IO</span>.<span style="color:#9900CC;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;TestGrep.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>line<span style="color:#006600; font-weight:bold;">|</span>
  ln = ln <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{ln}: #{line}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> regex.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/ruby-day-2/#comments" thr:count="1" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/ruby-day-2/feed/atom/" thr:count="1" />
		<thr:total>1</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Ruby, Day 1]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/ruby-days-1-2/" />
		<id>http://www.pvle.be/2011/05/ruby-days-1-2/</id>
		<updated>2011-05-15T12:13:39Z</updated>
		<published>2011-05-15T10:04:24Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Ruby" /><category scheme="http://www.pvle.be" term="Seven Languages in Seven Weeks" />		<summary type="html"><![CDATA[It’s funny because I never learned Ruby, I never wanted to because dynamic languages don’t move me much, but so far it’s been quite fun. First, a link to http://ruby-doc.org/docs/ProgrammingRuby/ that is the main reference to every question on that language. I’m not going to run through all the exercises, but I will put solutions [...]]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/ruby-days-1-2/"><![CDATA[<p>It’s funny because I never learned Ruby, I never wanted to because dynamic languages don’t move me much, but so far it’s been quite fun.</p>
<p>First, a link to <a href="http://ruby-doc.org/docs/ProgrammingRuby/">http://ruby-doc.org/docs/ProgrammingRuby/</a> that is the main reference to every question on that language.</p>
<p>I’m not going to run through all the exercises, but I will put solutions of those I found interesting to solve.</p>
<p>Print “This is sentence number 1” where the number 1 changes from 1 to 10:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</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> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;This is sentence number #{i}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Bonus problem: write a program that picks a random number (I will do between 1 and 1024, how geeky!). Let the player guess a number, telling the player whether the guess is too low or too high:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">max = <span style="color:#006666;">1024</span>
&nbsp;
guess = <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
number = <span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span>max<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Guess a number from 1 to #{max}&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">until</span> guess == number
  guess = <span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#9900CC;">to_i</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Number is greater than #{guess}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> number <span style="color:#006600; font-weight:bold;">&gt;</span> guess
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Number is lesser than #{guess}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> number <span style="color:#006600; font-weight:bold;">&lt;</span> guess
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Well done!&quot;</span></pre></div></div>

]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/ruby-days-1-2/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/ruby-days-1-2/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Seven Languages in Seven Weeks]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/seven-languages-in-seven-weeks/" />
		<id>http://www.pvle.be/2011/05/seven-languages-in-seven-weeks/</id>
		<updated>2011-05-15T09:46:01Z</updated>
		<published>2011-05-15T09:46:01Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="Seven Languages in Seven Weeks" />		<summary type="html"><![CDATA[A recently bought the book titles “Seven Languages in Seven Weeks”. The book has excellent reviews, and I have to say that in the list of languages it covers, I only have vague notions for some of them (Prolog, Clojure, Haskell), but absolutely no experience with any of them. So, what I’m going to do [...]]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/seven-languages-in-seven-weeks/"><![CDATA[<p>A recently bought the book titles “<a href="http://pragprog.com/titles/btlang/seven-languages-in-seven-weeks">Seven Languages in Seven Weeks</a>”. The book has excellent reviews, and I have to say that in the list of languages it covers, I only have vague notions for some of them (Prolog, Clojure, Haskell), but absolutely no experience with any of them.</p>
<p>So, what I’m going to do is write my answers to the exercises on this blog, day by day (the book is divided by “days”) to keep track of my progress and findings. I started out by writing all of this in a text file for private use, but I figured that, after all, why not make it public?</p>
<p>This will probably look boring if you know any of these languages, but mind you that I have no experience whatsoever with nearly all of them.</p>
<p>So, let’s start with the very trendy Ruby!</p>
]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/seven-languages-in-seven-weeks/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/seven-languages-in-seven-weeks/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>Philippe</name>
						<uri>http://www.pvle.be</uri>
					</author>
		<title type="html"><![CDATA[Google Mail Ads]]></title>
		<link rel="alternate" type="text/html" href="http://www.pvle.be/2011/05/google-mail-ads/" />
		<id>http://www.pvle.be/2011/05/google-mail-ads/</id>
		<updated>2011-05-06T13:37:21Z</updated>
		<published>2011-05-06T13:37:21Z</published>
		<category scheme="http://www.pvle.be" term="Uncategorized" /><category scheme="http://www.pvle.be" term="C#" /><category scheme="http://www.pvle.be" term="Google" />		<summary type="html"><![CDATA[Just a fun thing that happened today: I was sending a mail in GMail, and noticed that the customized ads displayed something that could be considered “offensive” to me, in a fun way: Mr. Google is lying. C# rocks!]]></summary>
		<content type="html" xml:base="http://www.pvle.be/2011/05/google-mail-ads/"><![CDATA[<p>Just a fun thing that happened today: I was sending a mail in GMail, and noticed that the customized ads displayed something that could be considered “offensive” to me, in a fun way:</p>
<p><a href="http://www.pvle.be/wp-content/2011/05/CSharpSucksAd.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="CSharpSucksAd" border="0" alt="CSharpSucksAd" src="http://www.pvle.be/wp-content/2011/05/CSharpSucksAd_thumb.png" width="283" height="203" /></a></p>
<p>Mr. Google is lying. C# <strong>rocks</strong>!</p>
]]></content>
		<link rel="replies" type="text/html" href="http://www.pvle.be/2011/05/google-mail-ads/#comments" thr:count="0" />
		<link rel="replies" type="application/atom+xml" href="http://www.pvle.be/2011/05/google-mail-ads/feed/atom/" thr:count="0" />
		<thr:total>0</thr:total>
	</entry>
	</feed>

