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

<channel>
	<title>RailsRocket - Ruby on Rails Tutorials</title>
	<atom:link href="http://www.railsrocket.com/articles/feed" rel="self" type="application/rss+xml" />
	<link>http://www.railsrocket.com/articles</link>
	<description>A Ruby on Rails community website with tutorials</description>
	<pubDate>Tue, 18 Nov 2008 20:14:15 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>The Controversial Eval Function</title>
		<link>http://www.railsrocket.com/articles/the-controversial-eval-function</link>
		<comments>http://www.railsrocket.com/articles/the-controversial-eval-function#comments</comments>
		<pubDate>Tue, 11 Nov 2008 20:15:28 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[dangerous code]]></category>

		<category><![CDATA[design]]></category>

		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=200</guid>
		<description><![CDATA[eval is a function in Ruby that allows you to execute arbitrary code. You pass in a string containing code, and voila! It's very useful, for things like holding pointers of a sort to functions; but it's also dangerous, because script-kiddies can format your hard-drive. So what's the middle-path use-case of eval?]]></description>
			<content:encoded><![CDATA[<p>Today we&#8217;re going to discuss one of those highly-controversial hot topics in <a href="ruby-crash-course"  class="alinks_links" onclick="return alinks_click(this);" title="Check out our Ruby crash-course!"  >Ruby</a> (if there can <em>be</em> such a thing as a &#8220;hot topic&#8221; in programming languages)&#8211;the <code><a href="the-controversial-eval-function"  class="alinks_links" onclick="return alinks_click(this);" title="executes arbitrary code"  >eval</a></code> function.</p>
<p>What does <code>eval</code> do? Quite simply, you pass in some Ruby code in a string, and it&#8217;ll evaluate it. Observe:</p>
<p><code>eval "puts 2+2" # =&gt; 4<br />
eval "'hello world!'.upcase # =&gt; HELLO WORLD</code></p>
<p>Clearly, this is useful stuff&#8211;you can pass in arbitrary code, even assemble your strings on the fly for <em>truly</em> complicated code.</p>
<p>But, eval is a dangerous tool&#8211;because it can execute <em>arbitrary</em> code. For example, if you have some code like this:</p>
<p><code>input = # ... read from some form<br />
eval input<br />
# ...</code></p>
<p>Some clever script kiddie will send the input <code>system 'rm -rf *'</code>, you can kiss your application goodbye. And your family photo-collection. And your MP3 collection. And your hard-drive.</p>
<p>Some people brand eval as an &#8220;evil,&#8221; &#8220;insidious,&#8221; &#8220;destructive&#8221; function. Which is true&#8211;it <em>has</em> destructive potential. That&#8217;s why you need to <em>sanitize</em> your input. Don&#8217;t just send it as-is. Don&#8217;t risk it.</p>
<p>On the other hand, eval has a lot of tricky uses for things you might not otherwise be able to do. For example, how would you create a generic &#8220;shutdown hook&#8221; system for your Ruby application? other programming languages have pointers&#8211;in .NET, for example, you can accumulate a collection of pointers, and run those. What can you do in Ruby? You can accumulate a collection of strings to eval!</p>
<p><code>shutdown_hooks = ["Logger.write 'normal termination'", "Logger.close", "DatabaseConnector.close"]</code></p>
<p>Voila! You can iterate through them and execute them, just like any set of pointers in other languages.</p>
<p>To recap:</p>
<ul>
<li>eval is merely a tool. It can be used, and abused, like any tool.</li>
<li>Use eval as necessary, but avoid it if possible.</li>
<li><em>Sanitize</em> your input! Don&#8217;t risk it! Script kiddies abound!
</ul>
<p>In other programming languages, one of the concerns raised with reflection is that it&#8217;s a &#8220;heavy&#8221; process. How does Ruby stack up? If you know, share it in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/the-controversial-eval-function/feed</wfw:commentRss>
		</item>
		<item>
		<title>Block Comments in Ruby</title>
		<link>http://www.railsrocket.com/articles/block-comments-in-ruby</link>
		<comments>http://www.railsrocket.com/articles/block-comments-in-ruby#comments</comments>
		<pubDate>Wed, 05 Nov 2008 22:22:40 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[comments]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[whitespace]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=196</guid>
		<description><![CDATA[How do you comment out a chunk of code? Other programming languages (C++, Java, etc.) have a slash-star style of block comments, like so: /* */ ... but what about Ruby? Does Ruby have a mechanism for this, too? The answer is YES; BUT, there's a catch! If you don't watch whitespace ...]]></description>
			<content:encoded><![CDATA[<p>This is an easy one. How do you comment out a chunk of code? Other programming languages (C++, Java, etc.) have a slash-star style of block comments, like so:</p>
<p><code>/*<br />
	This is a block comment. You can put whatever you want here.<br />
	Even ASCII art! For REAL!  o[+++]XXXXXXXXXXXX&gt;<br />
*/<br />
	// ...</code></p>
<p>But what about <a href="ruby-crash-course"  class="alinks_links" onclick="return alinks_click(this);" title="Check out our Ruby crash-course!"  >Ruby</a>?</p>
<p>Ruby has two special tags that you use: <code>=begin</code> and <code>=end</code>. So your Ruby comment would look like this:</p>
<p><code>=begin<br />
	This function returns the <a href="rails-21-aggregate-expressions"  class="alinks_links" onclick="return alinks_click(this);" title="minimum is an aggregate function in ActiveRecord"  >minimum</a> of A or B.<br />
	For example, if A is 3 and B is 7, it'll return 3.<br />
=end<br />
def self.min(a, b)<br />
	# ...<br />
end</code></p>
<p>Yes, yes! It&#8217;s easy!</p>
<p>Wait! What&#8217;s that, you say? You say it <em>doesn&#8217;t work</em>? Well, that&#8217;s tragic&#8211;and in fact, it happened to me, too. The key is that <strong>these tokens cannot be preceded by whitespace.</strong> If they are, it won&#8217;t work.</p>
<p>What? Ghetto, you say? Well, tough luck, my friend&#8211;this is how Ruby deals with block comments. Just use it, OK? It&#8217;s easier than commenting out 130+ lines of code one by one. (Or if you have an IDE that does it in one-shot, great, even better!)</p>
<p>And that&#8217;s Ruby comment blocks! Simple!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/block-comments-in-ruby/feed</wfw:commentRss>
		</item>
		<item>
		<title>Regular Expressions in Ruby (and Rails)</title>
		<link>http://www.railsrocket.com/articles/regular-expressions-in-ruby-and-rails</link>
		<comments>http://www.railsrocket.com/articles/regular-expressions-in-ruby-and-rails#comments</comments>
		<pubDate>Tue, 28 Oct 2008 19:56:51 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[regular expressions]]></category>

		<category><![CDATA[string replacement]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=193</guid>
		<description><![CDATA[How does Rails, or rather, Ruby, deal with Regular Expressions? There are a few things; first, the syntax for regular expressions is a bit unique in Ruby. Second, the String class has a few methods that you can use for regular-expression use--namely match and gsub, which you can use to match (groups) and access matches.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re new to <a href="regular-expressions-in-ruby-and-rails"  class="alinks_links" onclick="return alinks_click(this);" title="How does Rails deal with regular expressions?"  >Regular Expressions</a>, well, go read <a href="http://www.regular-expressions.info/">regular-expressions.info</a>, then come back.</p>
<p>If you&#8217;re wondering how Rails (and, consequentially, <a href="ruby-crash-course"  class="alinks_links" onclick="return alinks_click(this);" title="Check out our Ruby crash-course!"  >Ruby</a>) deals with Regular Expressions, you&#8217;ve come to the right place.</p>
<h2>Syntax</h2>
<p>Regular expressions in Rails are bracketed by forward-slash, so a <a href="regular-expressions-in-ruby-and-rails"  class="alinks_links" onclick="return alinks_click(this);" title="How does Rails deal with regular expressions?"  >regular expression</a> looks like this: <code>/[0-9]*/</code>. You can put all your usual modifiers after the second slash (such as <code>i</code> for case-insensitivity). Gone are other programming languages&#8217; ways of dealing with regular expressions as a string!</p>
<h2>Matching Expressions</h2>
<p>As you&#8217;d expect, you <a href="regular-expressions-in-ruby-and-rails"  class="alinks_links" onclick="return alinks_click(this);" title="A function to match regular expressions"  >match</a> expressions against string. Hence the <code>String</code> class has a <code>match</code> function that takes a regular expression, and returns the partially-matched string.</p>
<p><code>"Hello".match(/l/) =&gt; "l"</code></p>
<p>Great stuff! You can do all your usual complex matching. And if nothing is matched, you get nil:</p>
<p><code>"Hello".match(/x/) =&gt; nil</code></p>
<p>This allows you to easily write <a href="validation-101"  class="alinks_links" onclick="return alinks_click(this);" title="Rails supports model-level validation"  >validation</a> code (in Ruby, since Rails has its own Validation framework) like so:</p>
<p><code>if credit_card.match(/[0-9]{16}/).nil?<br />
	puts "Please enter a valid 16-digit credit-card number!"<br />
else<br />
	# process order<br />
end</code></p>
<h2>Replacing Matched Groups</h2>
<p>As mentioned above, you can check for matches by checking if <code>match</code> returns <code>nil</code>. But what if you want to replace a matched piece of text with something else?</p>
<p>For example, with credit-card storage, it&#8217;s a common thing that you want to cross out the first 12 digits, and only print the last four. In Regular Expression parlance, these groups are called &#8220;backreferences,&#8221; because they refer <em>back</em> to what you match.</p>
<p>Check this example out, where we have four groups:</p>
<p><code>VALID_CREDIT_CARD = /([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{4})/</code></p>
<p>If you wanted to access the first group of four digits, you write <code>\1</code> (or, in a string, <code>\\1</code>, to back-quote the slash). The second set is <code>\2</code>, and so on.</p>
<p>How can you use this? Say you want to replace the middle two groups with &#8220;XXXX&#8221;; you can use <code>string.<a href="string-replacement-in-ruby"  class="alinks_links" onclick="return alinks_click(this);" title="globlal string replacement"  >gsub</a></code>, which takes a regular expression, and performs a replacement, like so:</p>
<p><code>"1234-5678-9012-3456".gsub(VALID_CREDIT_CARD, "\\1-XXXX-XXXX-\\2") =&gt; 1234-XXXX-XXXX-3456</code></p>
<p>Needless to say, the possibilities are endless. You can create almost anything you can imagine with this simple tool&#8211;everything from credit-card hiding to a notation language like RedCloth to an HTML cleanup tool. Stretch your imagination!</p>
<p>And in fact, this is the core functionality in WebWrench, our Ruby static website generation tool. Check it out, and see what you learn.</p>
<p>And that&#8217;s regular expressions &#8230; the Ruby/Rails way!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/regular-expressions-in-ruby-and-rails/feed</wfw:commentRss>
		</item>
		<item>
		<title>Conditional Code</title>
		<link>http://www.railsrocket.com/articles/conditional-code</link>
		<comments>http://www.railsrocket.com/articles/conditional-code#comments</comments>
		<pubDate>Tue, 21 Oct 2008 20:08:54 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[control flow]]></category>

		<category><![CDATA[debugging]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=186</guid>
		<description><![CDATA[You're writing some code for a new client. Debug statements litter the code. Oops, you sent that to the client. Oops, you're printing out credit-card numbers. Identity theft ensues. Don't move to Mexico--you can actually handle this problem in an elegant way by using conditional code statements INSIDE your class ...]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re writing a small web application for a new client. You&#8217;re wrestling with the credit-card processing module&#8211;it doesn&#8217;t do what you want. <code>puts</code> statements and temporary output litter your code. Three hours and eight coke-cans later, you solve it! Joyous, you wipe out the temporary code, and email it to the customer.</p>
<p>They reply back: it&#8217;s printing some temporary variables. Oops.</p>
<p>It&#8217;s printing out other credit-card numbers.</p>
<p>Identity theft ensues.</p>
<p>Double-oops.</p>
<p>Their email makes it clear: their lawyer will be in touch with you. You throw everything in a suitcase and bolt out the door.</p>
<p>Mexico, here I come!</p>
<p>&#8230;</p>
<p>There&#8217;s a common problem in software&#8211;how do you write code that only runs only in &#8220;debug&#8221; mode? We&#8217;re all used to that style of peppering our code with puts statements and temporary output. Learn how to use a debugger!</p>
<p>But there&#8217;s a deeper problem, one that large companies commonly face, too; how can you write debug-only code? For example, you might want to throw some functions in your class that print out extra functions, or bypass certain lengthy steps&#8211;just for debugging.</p>
<p><a href="ruby-crash-course"  class="alinks_links" onclick="return alinks_click(this);" title="Check out our Ruby crash-course!"  >Ruby</a> provides a surprisingly simple but powerful solution to this:</p>
<p>if statements.</p>
<p>Observe the code below:</p>
<p><code>class DataCruncher<br />
	def crunch_current_task<br />
		current_task = Tasks.find(:first, conditions =&gt; ['owner_id = ?', current_user.id])<br />
		current_task.start<br />
		# ...<br />
	end<br />
end</code></p>
<p>Say we wanted to print out the task ID and title in debug mode. Assuming you have DEBUG_MODE defined in your <code>environment.rb</code> file in your Rails application (or in a global constant somewhere, in Ruby), you can do this:</p>
<p><code>class DataCruncher<br />
	def crunch_current_task<br />
		current_task = # ...<br />
		if DEBUG_MODE<br />
			Logger.log("Starting task #{current_task.id}: #{current_task.name}")<br />
		end<br />
		# ...<br />
	end<br />
end</code></p>
<p>Let&#8217;s take it a step further. Say you wanted to create a <code>short_circuit</code> method, in debug mode ONLY, that makes sure the task finishes in five seconds. You can (surprisingly) do this:</p>
<p><code>class DataCruncher<br />
	if DEBUG_MODE<br />
		def short_circuit<br />
			current_task = # ...<br />
			current_task.time_to_process = 5<br />
		end</p>
<p>	def crunch_current_task<br />
		# as above<br />
	end<br />
end</code></p>
<p>The key here is that the if-statement is <strong>inside the class declaration, not inside a method declaration.</strong></p>
<p>What will this do? If you try to access <code>some_task.short_circuit</code>, in debug mode, it&#8217;ll work; but in non-debug mode, Rails will tell you the method doesn&#8217;t exist!</p>
<p>You can even take this a step further (although the design decision might not be great) in a multi-user system, and define different/specific functions that users can use, based on their current role. For example, administrative users might have access to methods like <code>flush_data</code>, <code>delete_user</code>, and fun things like that. Non-admin users not only don&#8217;t have access to those methods, but they don&#8217;t even know they exist!  The down side, of course, is rather than getting an &#8220;access denied&#8221; message, you get a &#8220;method not found&#8221; <a href="exception-and-error-handling"  class="alinks_links" onclick="return alinks_click(this);" title="How can you trap exceptions/errors in Ruby?"  >error</a>. Annoying to try and handle, considering it could pop up in any part of your code.</p>
<p>Still, this is a powerful and useful feature of Ruby that you can use to make your life a little easier.</p>
<p>And of course, printing debug statements is as easy as:</p>
<p><code><br />
class Debug<br />
	def print(message)<br />
		puts message unless System.DEBUG_MODE == false<br />
	end<br />
end</p>
<p>But the power and flexibility of conditional code is enormous; what are some fantastic use-cases for something like this? What kind of situations can it (or has it) really help you out in?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/conditional-code/feed</wfw:commentRss>
		</item>
		<item>
		<title>Bort: Better Skeleton Applications</title>
		<link>http://www.railsrocket.com/articles/bort-better-skeleton-applications</link>
		<comments>http://www.railsrocket.com/articles/bort-better-skeleton-applications#comments</comments>
		<pubDate>Thu, 09 Oct 2008 19:53:13 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Gems and Plugins]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=184</guid>
		<description><![CDATA[bort is a replacement for your standard Rails application skeleton. It comes with Restful Authentication, Will Paginate, code for using a MySQL database, database sessions, and more! Use it instead (it won't hurt you), and it'll help you bootstrap your Rails applications even faster (and in a better way)!]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re all familiar with the <code>rails</code> command-line tool. It generates a Rails skeleton application with everything you need to start your Rails application&#8211;directories, configuration, even a default index page.</p>
<p>But the more you develop Rails applications, the more you begin to realize that some tools and gems are valuable on <em>every</em> project. Need a user management system? Likely <code>restful_authentication</code>. Rails pagination slow? Grab <code>will_paginate</code>. Even things like setting sessions to use the database, or using <a href="mysql-configuration-for-rails"  class="alinks_links" onclick="return alinks_click(this);" title="Read our config sample for a MySQL DB!"  >MySQL</a> instead of sqlite3 as your database.</p>
<p>Enter <a href="bort-better-skeleton-applications"  class="alinks_links" onclick="return alinks_click(this);" title="A bootstrapped Rails skeleton application"  >bort</a>. Bort is a replacement of the standard skeleton application; it includes these features (and more), already setup and configurated to work out-of-the-box. And it <em>is</em> a bigger box&#8211;it includes:</p>
<ul>
<li><strong>RESTful Authentication</strong>, for a RESTful user-management system (registering users, logging in, etc.). Even includes the forgotten password feature!</li>
<li><strong>User Roles</strong>, to make your application users easier to manage; includes a default admin role and an admin user.</li>
<li><strong>Will Paginate</strong>, an efficient, pretty pagination plugin. Beats the standard Rails pagination by ages!</li>
<li><strong>OpenID integration</strong>, in case you want to support OpenID with your users.</li>
<li><strong>Rspec</strong>, a better way to write tests.</li>
<li><strong><a href="exception-and-error-handling"  class="alinks_links" onclick="return alinks_click(this);" title="How can you trap exceptions/errors in Ruby?"  >Exception</a> Notifier</strong>, to email you when your application hiccups.</li>
<li><strong>Asset Packager</strong>, which combines javascript and CSS to save download time.</li>
<li><strong>Database Sessions, MySQL DB YAML, an application-wide layout file, and no default index.html and rails.png</strong>, all of which make your life a little easier.</li>
</ul>
<p>This is a big step up from your standard Rails skeleton. Best of all, if you don&#8217;t use half of this stuff, it won&#8217;t affect you much; and it&#8217;ll be there when you need it.</p>
<p>So why wait? Grab bort from the <a href="http://github.com/fudgestudios/bort/tree/master">GitHub page</a>, and enjoy easier, quicker, better Rails applications&#8211;for free!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/bort-better-skeleton-applications/feed</wfw:commentRss>
		</item>
		<item>
		<title>CSS Progress Bar</title>
		<link>http://www.railsrocket.com/articles/css-progress-bar</link>
		<comments>http://www.railsrocket.com/articles/css-progress-bar#comments</comments>
		<pubDate>Tue, 30 Sep 2008 17:11:05 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[css]]></category>

		<category><![CDATA[launchpad]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=161</guid>
		<description><![CDATA[How can you create a progress-bar with pure CSS, no images? In this post, we discuss one such attempt by Launchpad--where we have a target article-count goal, which we display in the dashboard. And we use a progress bar that's pure CSS.]]></description>
			<content:encoded><![CDATA[<p>One of the unique features of <a href="launchpad"  class="alinks_links" onclick="return alinks_click(this);" title="A RailsRocket project: an article CMS"  >Launchpad</a> is the article <a href="rails-21-aggregate-expressions"  class="alinks_links" onclick="return alinks_click(this);" title="count is an aggregate function in ActiveRecord"  >count</a> meter. Since Launchpad is geared towards being a community-driven article website, it has an &#8220;article count&#8221; goal. The idea is that the target moves; it grows, exponentially&#8211;because as your site gets popular, more and more people should contribute.</p>
<p>Having said this, we display the article count as a progress bar in the dashboard. But how can we have a dynamic progress bar with no images&#8211;100% CSS?</p>
<p>The solution is a clever bit of CSS; we have a fixed-width we decide on for the bar (eg. 600px), and we make a bordered div to represent the bar; inside, we have the &#8220;progress&#8221; area in a second div. We calculate the width of that area based on the number of articles.</p>
<p>Here&#8217;s the code:</p>
<p><code>&lt;div style="width: &lt;%= (600 - (Article.percentage_of_goal_count * 6))%&gt;px; background: #DDD; border: 2px solid black; height: 20px;"&gt;<br />
&lt;div style="text-align: right; float: left; background: #0B0; width: &lt;%= (Article.percentage_of_goal_count * 6) %&gt;px;"&gt;<br />
&lt; span style="margin-right: 6px; color: white; font-weight: bold;"&gt;&lt;%= Article.public_count %&gt; / &lt;%= Article.goal_count %&gt; (&lt;%= (Article.percentage_of_goal_count) %&gt;%)</span><br />
&lt;/div&gt;<br />
&lt;/div&gt;</code></p>
<p>(<code>goal_count</code> is the number of articles in the current goal, eg. 500; and <code>percentage_of_goal_count</code> is the percent of articles we have of our goal; so 50/500 is 10.)</p>
<p>And it works! Because you have the widths &#8220;hard-coded&#8221;, it looks beautiful in all browsers. The CSS is simple, with only some floats and text-aligns. Even the text percentage is selectable!</p>
<p>Are there any short-comings? Unlike a more advanced progress bar, the text colour doesn&#8217;t invert in the progress area. Other than that, great!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/css-progress-bar/feed</wfw:commentRss>
		</item>
		<item>
		<title>String Replacement in Ruby</title>
		<link>http://www.railsrocket.com/articles/string-replacement-in-ruby</link>
		<comments>http://www.railsrocket.com/articles/string-replacement-in-ruby#comments</comments>
		<pubDate>Thu, 25 Sep 2008 11:55:31 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[regular expressions]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[string functions]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=144</guid>
		<description><![CDATA[How can you replace strings in Ruby? There are a couple of options; there's the tr function, which takes two sets of characters, and substitutes them; and there's the gsub method, which makes a global replacement based on a regular expression. Powerful stuff!]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re used to other programming languages, you&#8217;re likely looking for some sort of <code>replace</code> function in your strings to replace bits with other bits. But skimming through the <a href="http://www.ruby-doc.org/core/classes/String.html">String RDoc</a>, you don&#8217;t find anything like that, right?</p>
<p>There are a couple of options you have available to you. The first is the <code><a href="string-replacement-in-ruby"  class="alinks_links" onclick="return alinks_click(this);" title="a character-replacement function"  >tr</a></code> function; you pass in sets of characters, and it replaces them with other characters. For example, if you wanted to replace all vowels with an asterick, you could write:</p>
<p><code>"hello".tr("aeiou", "*") # =&gt; "<a href="the-mysterious-h-function"  class="alinks_links" onclick="return alinks_click(this);" title="The h function escapes string special characters"  >h</a>*ll*</code></p>
<p>On the other hand, if you wanted to remove all kinds of symbols&#8211;something useful&#8211;you could write something like:</p>
<p><code>"test: !@#$%^&amp;*()_+ done!".tr("!@#$%^&amp;*()_+", "") # =&gt; "<a href="testing-rails-applications"  class="alinks_links" onclick="return alinks_click(this);" title="Rails makes it easy to write tests!"  >test</a>: done"</code></p>
<p>Clearly, some powerful stuff.</p>
<p>But what if you want a more <em>natural</em> approach to string replacement&#8211;replacing words or string fragments with other words or string fragments? For example, maybe you want to replace all instances of the bold tag with the strong tag.</p>
<p>You can use the <code><a href="string-replacement-in-ruby"  class="alinks_links" onclick="return alinks_click(this);" title="globlal string replacement"  >gsub</a></code> method; it takes a <a href="regular-expressions-in-ruby-and-rails"  class="alinks_links" onclick="return alinks_click(this);" title="How does Rails deal with regular expressions?"  >regular expression</a>, and does a global (string-wide) replacement. Let&#8217;s say we want to remove all numbers from a string. We can do it with <code>tr</code>; but what about removing all two-digit numbers? <code>gsub</code> to the <a href="exception-and-error-handling"  class="alinks_links" onclick="return alinks_click(this);" title="Keyword used to trap errors"  >rescue</a>!</p>
<p><code>"There were 13 hamsters, 29 dogs, 3 cats, 5 melons, and 72 mice.".gsub(/[0-9][0-9]/, 'some') # =&gt; "There were some hamsters,  some dogs, 3 cats, 5 melons, and some mice."</code></p>
<p>Since it uses <a href="regular-expressions-in-ruby-and-rails"  class="alinks_links" onclick="return alinks_click(this);" title="How does Rails deal with regular expressions?"  >regular expressions</a>, you can do all kinds of strange and complicated stuff. The limitations are only your imagination (and regular-expression capability)!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/string-replacement-in-ruby/feed</wfw:commentRss>
		</item>
		<item>
		<title>Ruby Command-Line Arguments</title>
		<link>http://www.railsrocket.com/articles/ruby-command-line-arguments</link>
		<comments>http://www.railsrocket.com/articles/ruby-command-line-arguments#comments</comments>
		<pubDate>Sat, 20 Sep 2008 20:25:01 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[command-line]]></category>

		<category><![CDATA[environment]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=137</guid>
		<description><![CDATA[How can you access command-line arguments in Ruby? Through the special array called ARGV. It's a zero-indexed array of your command-line arguments; so ARGV[0] is the first, ARGV[1] is the second, and so on. You can use ARGV.length to get the number of arguments, too.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re writing a serious <a href="ruby-crash-course"  class="alinks_links" onclick="return alinks_click(this);" title="Check out our Ruby crash-course!"  >Ruby</a> application (not a Rails application necessarily), this question may apply to you: how do you access command-line arguments in Ruby?</p>
<p>Ruby provides an array called <code><a href="ruby-command-line-arguments"  class="alinks_links" onclick="return alinks_click(this);" title="an array of command-line arguments"  >ARGV</a></code> (all upper-case). This array contains all the command-line arguments; so if you run this:</p>
<p><code>ruby script.rb first second third</code></p>
<p>&#8230; then <code>ARGV.length</code> is 3, <code>ARGV[0]</code> is &#8220;first&#8221;, <code>ARGV[1]</code> is second, and <code>ARGV[2]</code> is third.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/ruby-command-line-arguments/feed</wfw:commentRss>
		</item>
		<item>
		<title>Launchpad</title>
		<link>http://www.railsrocket.com/articles/launchpad</link>
		<comments>http://www.railsrocket.com/articles/launchpad#comments</comments>
		<pubDate>Wed, 17 Sep 2008 14:07:33 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=171</guid>
		<description><![CDATA[Launchpad is an open-source article-based CMS and blogging platform for Rails.  It has all the features you come to expect from a CMS, like admin-only edit links, commenting, voting, RSS feeds, future-publishing posts, and user registration.  Best of all, it's written 100% in Rails!]]></description>
			<content:encoded><![CDATA[<h2>The Summary</h2>
<p><a href="launchpad"  class="alinks_links" onclick="return alinks_click(this);" title="A RailsRocket project: an article CMS"  >Launchpad</a> is an open-source article-based CMS and blogging platform for Rails.  It has all the features you come to expect from a CMS, like admin-only edit links, commenting, voting, <a href="creating-rss-feeds"  class="alinks_links" onclick="return alinks_click(this);" title="You can easily create your own RSS feeds with Rails"  >RSS</a> feeds, future-publishing posts, and user registration.  Best of all, it&#8217;s written 100% in Rails!</p>
<h2>The Problem</h2>
<p>You want to create a flexible CMS system.  You want users to be able to register and add content, and people to view that content; you want all the usual (comments, voting on articles, RSS feeds, etc.)</p>
<h2>The Solution</h2>
<p>Rails makes it fairly easy to write your own CMS.  You can probably use <a href="scaffolding-in-rails-20"  class="alinks_links" onclick="return alinks_click(this);" title="Scaffolding changed in Rails 2.0"  >scaffolding</a> for most of the administrative pages (and pretty them up later, if you want multiple users to register and contribute).  The business logic of the application is pretty simple.</p>
<h2>The Articles</h2>
<ul>
<li>Coming Soon!</li>
</ul>
<h2>The Architecture</h2>
<p>At the core, you have one model: an article (which doubles as a page, too, for static-page content, like an About page).  You also might have votes and comments as models.  You can use scaffolding for most of the code; this will make the forms free for new, edit, etc. (which is most of the work); you can pretty those up.  The main page will be the view-articles page (and maybe a front page; that will require a bit of work).  You can handle RSS feeds through the XML builder.</p>
<h2>The Features</h2>
<p>Launchpad includes the following features:</p>
<ul>
<li><strong>Voting and Commenting:</strong> users can vote and comment on articles (<a href="rails-21-aggregate-expressions"  class="alinks_links" onclick="return alinks_click(this);" title="maximum is an aggregate function in ActiveRecord"  >maximum</a> one vote per user per article).</li>
<li><strong>RSS Feeds:</strong> there&#8217;s an article RSS feed, but also, each posts&#8217; comments has an RSS feed.</li>
<li><strong>Dashboard:</strong> the dashboard provides a quick summary of what&#8217;s going on&#8211;new and upcoming posts, as well as articles and votes.</li>
<li><strong>Registration:</strong> Other users and contributers can register (but as admins only; see shortcomings)</li>
<li><strong>Post URL:</strong> like Wordpress, you can edit the &#8220;post slug&#8221; or post URL of posts!</li>
<li><strong>Future-Posting:</strong> like Wordpress, set the publication date of an article in the future, and it&#8217;ll appear on that date.</li>
</ul>
<h2>The Short-Comings</h2>
<p>Launchpad isn&#8217;t intended to be a perfect, polished, public project; so it has a few drawbacks:</p>
<ul>
<li><strong>Hard-coded Admin:</strong> the administrative user is hard-coded as the user with an ID of 1.</li>
<li><strong>No Roles:</strong> there are no roles (contributer, editor, administrator); if you&#8217;re registered, you&#8217;re considered an admin.</li>
<li><strong>Few Tests:</strong> this was one of our first projects!  It has a fair bit of <a href="testing-rails-applications"  class="alinks_links" onclick="return alinks_click(this);" title="Rails has built-in testing!  It's easy to set up!"  >testing</a>, but by and far, much of the application (especially the view HTML) is not tested.</li>
<li><strong>Non-Pluggable, Non-Skinnable:</strong> You can&#8217;t write plugins, and you can&#8217;t create skins; you can only edit the layouts.</li>
<li><strong>No Spam Filtering:</strong> you can hardly launch without this!</li>
</ul>
<h2>The Code</h2>
<p>You can get the latest code from the <a href="http://launchpad.rubyforge.org/">RubyForge page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/launchpad/feed</wfw:commentRss>
		</item>
		<item>
		<title>Rails 2.1: Increment and Decrement Value</title>
		<link>http://www.railsrocket.com/articles/rails-21-increment-and-decrement-value</link>
		<comments>http://www.railsrocket.com/articles/rails-21-increment-and-decrement-value#comments</comments>
		<pubDate>Mon, 15 Sep 2008 08:14:13 +0000</pubDate>
		<dc:creator>ashes999</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[activerecord]]></category>

		<category><![CDATA[rails 2.1]]></category>

		<guid isPermaLink="false">http://www.railsrocket.com/articles/?p=133</guid>
		<description><![CDATA[Prior to Rails 2.1, the increment and decrement functions needed to be called multiple times if you wanted to increment/decrement by more than one. In Rails 2.1, you can pass in a second parameter: a value, how much to increment or decrement the field by. (And it can take negative values.)]]></description>
			<content:encoded><![CDATA[<p>Rails allows you to easily <a href="rails-21-increment-and-decrement-value"  class="alinks_links" onclick="return alinks_click(this);" title="ActiveRecord fields are incrementable"  >increment</a> and <a href="rails-21-increment-and-decrement-value"  class="alinks_links" onclick="return alinks_click(this);" title="ActiveRecord fields are decrementable"  >decrement</a> fields on your ActiveRecord objects, like so:</p>
<p><code>page.increment!(:num_views)<br />
ad.decrement!(:views_left)</code></p>
<p>But what if you want to increment or decrement by more than one? Prior to <a href="rails-21-ebook"  class="alinks_links" onclick="return alinks_click(this);" title="Check out the free Rails 2.1 ebook!"  >Rails 2.1</a>, you needed to do this manually:</p>
<p><code>comment.increment!(:spam_score)<br />
comment.increment!(:spam_score)<br />
comment.increment!(:spam_score)</code></p>
<p>&#8230; would increment by three. In Rails 2.1, increment and decrement both take a second (optional) value of how much to increment or decrement by:</p>
<p><code>comment.increment!(:spam_score, 3)</code></p>
<p>And yes, you can pass in negative values (though why would you, if you have both increment <em>and</em> decrement functions?)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsrocket.com/articles/rails-21-increment-and-decrement-value/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
