<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Ruby School Dropout - Home</title>
  <id>tag:rubyschooldropout.com,2007:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://rubyschooldropout.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://rubyschooldropout.com/" rel="alternate" type="text/html"/>
  <updated>2007-09-23T21:23:51Z</updated>
  <entry xml:base="http://rubyschooldropout.com/">
    <author>
      <name>dave</name>
    </author>
    <id>tag:rubyschooldropout.com,2007-09-23:9</id>
    <published>2007-09-23T17:34:00Z</published>
    <updated>2007-09-23T21:23:51Z</updated>
    <link href="http://rubyschooldropout.com/2007/9/23/file-manipulation-in-action" rel="alternate" type="text/html"/>
    <title>File Manipulation In Action</title>
<content type="html">
            &lt;p&gt;Ok, time to roll up the sleeves and get started with that &lt;a href=&quot;http://www.rubyschooldropout.com/2007/9/6/thinking-through-some-ruby-file-manipulation&quot;&gt;previous need&lt;/a&gt;. So that first part:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Find all files in a remote directory that match ~somepathhere/*htm/printable&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Got it on the first try, thanks to Why&#8217;s (poignant) guide:&lt;/p&gt;&lt;/p&gt;

&lt;pre&gt;p Dir['*.htm/printable']&lt;/pre&gt;

&lt;p&gt;I think i&#8217;ll bust up the order and maybe work on changing the filenames next, cause I have an inkling of how to do that. More in a bit.&lt;/p&gt;

&lt;p&gt;Ok, next I&#8217;m working on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;rename each from /printable to /index.html&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I tried this:&lt;/p&gt;

&lt;pre&gt;Dir['*.htm/printable'].each do |file_name|
File::rename(file_name, 'index.html')
end&lt;/pre&gt;

&lt;p&gt;but that ended up ignoring the paths and saving all the files on top of each other as index.html where I ran the script. So now I&#8217;m trying:&lt;/p&gt;

&lt;pre&gt;Dir['*.htm/printable'].each do |old_file_path|
new_file_path = old_file_path.sub('printable','index.html')
File::rename(old_file_path, new_file_path)
end&lt;/pre&gt;

&lt;p&gt;looks like it worked, except it choked at the &#8220;p&#8220;&#8216;s where it looks like there&#8217;s an actual file name that contains &#8220;printable&#8221; but does not follow this pattern. Ok, just need to adjust the script to skip that case.&lt;/p&gt;

&lt;p&gt;I ended up making the pattern more precise by looking for /printable instead of just printable:&lt;/p&gt;

&lt;pre&gt;Dir['*.htm/printable'].each do |old_file_path| 
new_file_path = old_file_path.sub('/printable','/index.html')
File::rename(old_file_path, new_file_path)
end&lt;/pre&gt;

&lt;p&gt;Success!&lt;/p&gt;

&lt;p&gt;Next up is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;insert into the base href=&#8221;&#8221; my new desired base href&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;modify the path to the stylesheet in each to replace “../../” with the full path&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This should be fun. Some file readin&#8217; and file writin&#8217;. Back after a short break.&lt;/p&gt;

&lt;p&gt;Ok, I cheated a little. I couldn&#8217;t get it rocking with my own purely hand-written code. I think that&#8217;s because file.read() must send you the contents of the file as an array of lines, and I was treating it as one big string. I think I was close. But I ended up seeking help on the internet and adapting the recipe I found &lt;a href=&quot;http://pleac.sourceforge.net/pleac_ruby/fileaccess.html&quot;&gt;over here&lt;/a&gt; under &#8220;Modifying a File in Place Without a Temporary File&#8221;, like so:&lt;/p&gt;

&lt;pre&gt;old_base = 'BASE HREF=&quot;it was blank&quot;'
new_base = 'BASE HREF=&quot;my new base href&quot;'
old_style = 'HREF=&quot; path to the old stylesheet'
new_style = 'HREF=&quot;path to the new stylesheet&quot;'
Dir['*.htm/index.html'].each do |file_to_change|
  File.open(file_to_change,'r+') do |f|
    lines = f.readlines
    print lines
    lines.each do |it|
#make the substitutions
      it.gsub!(old_base, new_base)
      it.gsub!(old_style, new_style)
    end
#returns to line 0
    f.pos = 0
#writes the modified lines
    f.print lines
    f.truncate(f.pos)
    end
  end&lt;/pre&gt;

&lt;p&gt;I threw in &#8220;print lines&#8221; so I could see the code frantically crossing my terminal window. It all worked.&lt;/p&gt;

&lt;p&gt;Task completed, case closed. And I&#8217;m sure the need will arise again in the future for a batch search and replace.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://rubyschooldropout.com/">
    <author>
      <name>dave</name>
    </author>
    <id>tag:rubyschooldropout.com,2007-09-06:8</id>
    <published>2007-09-06T17:01:00Z</published>
    <updated>2007-09-06T17:08:44Z</updated>
    <link href="http://rubyschooldropout.com/2007/9/6/thinking-through-some-ruby-file-manipulation" rel="alternate" type="text/html"/>
    <title>Thinking Through Some Ruby File Manipulation</title>
<content type="html">
            &lt;p&gt;So a need has arisen. I&#8217;ll need to:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Find all files in a remote directory that match ~somepathhere/*htm/printable
&lt;li&gt;insert into the base href=&amp;quot;&amp;quot; my new desired base href
&lt;li&gt;modify the path to the stylesheet in each to replace &#8220;../../&#8221; with the full path
&lt;li&gt;rename each from /printable to /index.html
&lt;/ul&gt;

&lt;p&gt;I&#8217;m going to try to do this with Ruby. More as I get started.&lt;/p&gt;

&lt;p&gt;Oh, and I&#8217;ve switched books for the moment. For now I&#8217;m reading why&#8217;s (poignant) guide.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://rubyschooldropout.com/">
    <author>
      <name>dave</name>
    </author>
    <id>tag:rubyschooldropout.com,2007-04-14:7</id>
    <published>2007-04-14T23:53:00Z</published>
    <updated>2007-04-14T23:54:28Z</updated>
    <link href="http://rubyschooldropout.com/2007/4/14/not-given-up-not-gone-just-shifted" rel="alternate" type="text/html"/>
    <title>Not given up, not gone, just shifted</title>
<content type="html">
            &lt;p&gt;I&#8217;ll be picking this back up again soon. Had to switch over to more practical, hands-on learning and put down the booklearnin for a while. &lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://rubyschooldropout.com/">
    <author>
      <name>dave</name>
    </author>
    <id>tag:rubyschooldropout.com,2007-03-05:5</id>
    <published>2007-03-05T02:57:00Z</published>
    <updated>2007-03-05T03:00:56Z</updated>
    <link href="http://rubyschooldropout.com/2007/3/5/i-m-stuck-factorials-and-recursive-methods" rel="alternate" type="text/html"/>
    <title>I'm Stuck: Factorials and Recursive Methods</title>
<content type="html">
            &lt;p&gt;&lt;em&gt;Disclaimer and Preamble: Whenever I get stuck as I go through the books I&#8217;m using to learn Ruby on Rails and some other stuff, I&#8217;m going to write about it. All of the snags I hit and write about here may be excruciatingly elementary to you. Think of this as a public workbook. I&#8217;m writing up the roadblocks as a strategy to help myself stick with the process of learning. If I don&#8217;t figure it out in the act of writing it out here, I&#8217;m hoping someone else might come along and help me learn by catching the error in my approach or explaining something in different words. I&#8217;ll be delighted if you speak up and offer some help when you can, especially if you have an idea of what might get me to the understanding I need to continue.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;My one big rule is: Please don&#8217;t just tell me the answer outright without trying to help me learn it for myself. That won&#8217;t do me any good.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;My first snag in &lt;em&gt;Beginning Ruby on Rails&lt;/em&gt; was in one of the exercises at the end of Chapter 2. Yes, I&#8217;m doing all the exercises.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&#8221;Create a method that calls itself &#8211; a technique called &lt;em&gt;recursion&lt;/em&gt; &#8211; to calculate &lt;em&gt;factorials&lt;/em&gt;. A factorial is the product of the number times all the other whole numbers down to one &#8211; for example, the factorial of 6, written as 6!, is 6 x 5 x 4 x 3 x 2 x 1 = 720.&#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I&#8217;ve been scared of factorials ever since a gang of them made me quit math olympiad in middle school. And I hate how they&#8217;re always yelling. But conceptually I get them, and conceptually I get the idea of recursion. Nevertheless, I&#8217;m having trouble writing a recursive method to calculate them and have it work properly. Right here and now, I&#8217;m going to give it a fresh try and explain as I go, and if I get stuck you&#8217;ll see exactly where. So here we go.&lt;/p&gt;

&lt;p&gt;In words, I figure I need to count down to one(or up from one, if it&#8217;s easier) from the variable I want to factorialize, multiplying by the counting number as I go, and keeping track of that growing number. Right? As far as the recursive part, it seems like I&#8217;d create one method to multiply a number by it&#8217;s next lowest neighbor, as long as the number is a whole number greater than one. I could then call that method on itself as many times as needed to get to the victim of the factorializing. So if that&#8217;s all right, first thing I need is a method that takes N and turns it into N x (N-1).&lt;/p&gt;

&lt;p&gt;Here&#8217;s what I&#8217;m trying out for the first part:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def factorial_step(value)
value = value * (value - 1) unless value &amp;lt;=2
puts value
end
factorial_step(6)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Wow. Success! It returned &#8220;30&#8221;. This is already farther than I got the first time through. I may not need you after all. OK, so now I need to write a method that calls that method as many times as I need to get to N factorial. More after I think a minute and write my next small chunk.&lt;/p&gt;

&lt;p&gt;I think I need an iterator.&lt;/p&gt;

&lt;p&gt;Forgetting the recursive part for now and just trying to get it to work for a complete factorial, I tried:&lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;factorial_step&lt;/span&gt;(value)&lt;tt&gt;
&lt;/tt&gt;value.downto(&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; new_value = value * (value - &lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;puts new_value&lt;tt&gt;
&lt;/tt&gt;&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;factorial_step(&lt;span class=&quot;i&quot;&gt;6&lt;/span&gt;)&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;I got five 30&#8217;s back. So it&#8217;s looping the right number of times, at least. Now I need to get it to change the value of &#8220;value&#8221; each time through, and multiply it by the previous.&lt;/p&gt;

&lt;p&gt;And tweaking a little, now I&#8217;ve got&lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;factorial_step&lt;/span&gt;(value)&lt;tt&gt;
&lt;/tt&gt;value.downto(&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |new_value|&lt;tt&gt;
&lt;/tt&gt;new_value *= new_value - &lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;puts new_value&lt;tt&gt;
&lt;/tt&gt;&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;factorial_step(&lt;span class=&quot;i&quot;&gt;6&lt;/span&gt;)&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Which gives me 30, 20, 12, 6, 2&#8230;.Or in other words&#8230;(6 x 5), (5 x 4), (4 x 3), (3 x 2), (2 x 1). I&#8217;m flashing back to middle school. Feels like the factorials are getting me again. &lt;/p&gt;

&lt;p&gt;I think I have an extra step in here somewhere. I seem to be doing the multiplication twice and not serially. So let me rearrange here.&lt;/p&gt;

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td title=&quot;click to toggle&quot; class=&quot;line_numbers&quot;&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;factorial_step&lt;/span&gt;(value)&lt;tt&gt;
&lt;/tt&gt;total = &lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;value.downto(&lt;span class=&quot;i&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |new_value|&lt;tt&gt;
&lt;/tt&gt;total = new_value * total&lt;tt&gt;
&lt;/tt&gt;puts total&lt;tt&gt;
&lt;/tt&gt;&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;factorial_step(&lt;span class=&quot;i&quot;&gt;6&lt;/span&gt;)&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Now I&#8217;m multiplying each new value in the loop by whatever came before it. I think. And I get: 6, 30, 120, 360, 720.
Oh hell yeah. it&#8217;s working. Moving the &#8220;puts&#8221; line to where it needs to be to only show the final result is easy enough. My only question is, it works, but does it &#8220;call itself&#8221; like the assignment wanted? Not sure if it does.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://rubyschooldropout.com/">
    <author>
      <name>dave</name>
    </author>
    <id>tag:rubyschooldropout.com,2007-03-04:4</id>
    <published>2007-03-04T23:31:00Z</published>
    <updated>2007-03-04T23:52:40Z</updated>
    <link href="http://rubyschooldropout.com/2007/3/4/current-challenge-beginning-ror" rel="alternate" type="text/html"/>
    <title>Current Challenge: Beginning RoR</title>
<content type="html">
            &lt;p&gt;One of the books I'm working my way through right now is &lt;a href=&quot;http://www.wrox.com/WileyCDA/WroxTitle/productCd-0470069155.html&quot;&gt;Beginning Ruby on Rails&lt;/a&gt; by Steve Holzner, PhD. I didn't do much research on what to start with. I just walked in to Barnes and Noble and decided that I'd go with the best of the books they happened to have on hand at the time. I flipped to the Introduction, and sure enough the book told me it's for me: &quot;This book is for anyone who wants to develop online applications using Ruby and Rails. All you have to know is HTML to read this book profitably.&quot;&lt;/p&gt;

&lt;p&gt;Check. &lt;/p&gt;

&lt;p&gt;And so far, so good. I hit my first intellectual snag at the end of the second chapter, and that will be the subject of my very next entry here. Maybe you can help.&lt;/p&gt;

&lt;p&gt;One thing that's bugging me about the book is that the author uses limp, uninteresting example text and variables throughout the first couple of chapters -- lots of &quot;hello world&quot; and &quot;apples, oranges, and pears.&quot; Maybe his sense of humor doesn't match mine, or maybe he's just not letting his personality come through in his examples. You know, to keep the reader's attention. Through two chapters I've said hello from Ruby, said hello to my sweetie, and said hello in the style of bad John Wayne impressions. I've counted vegetables and checked the temperature to see if it would be appropriate for a picnic several times and in a number of ways. I understand the teaching method of using the same text through several examples, to help highlight how different approaches can accomplish the same results, but I wouldn't mind something other than salutations and the weather every once in a while. The tradition of making &quot;hello, world&quot; the first piece of code you write in a new language isn't lost on me, but that should only be example 1.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://rubyschooldropout.com/">
    <author>
      <name>admin</name>
    </author>
    <id>tag:rubyschooldropout.com,2007-02-28:1</id>
    <published>2007-02-28T03:41:00Z</published>
    <updated>2007-02-28T03:41:43Z</updated>
    <link href="http://rubyschooldropout.com/2007/2/28/first-entry-coming-soon" rel="alternate" type="text/html"/>
    <title>First entry coming soon.</title>
<content type="html">
            Promise.
          </content>  </entry>
</feed>
