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

<channel>
	<title>matthewcarriere.com &#187; console</title>
	<atom:link href="http://matthewcarriere.com/tag/console/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewcarriere.com</link>
	<description>a blog about startups, tech, and development.</description>
	<lastBuildDate>Sat, 12 May 2012 04:31:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Take it to the next level with the Ruby on Rails console</title>
		<link>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/</link>
		<comments>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 05:53:19 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=14</guid>
		<description><![CDATA[If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this. Here&#8217;s the scenario: You are writing code against a class and need to determine which attributes you have available to you. In Rails, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this.</p>
<p>Here&#8217;s the scenario:</p>
<p>You are writing code against a class and need to determine which attributes you have available to you. In Rails, your attributes are largely driven by the database schema, so you will not see them in the class definition. At this point most developers will take a look at the table in the database and find the column they need, then determine the type of data stored there. There is a better way&#8230;</p>
<p>Let&#8217;s use the command line.</p>
<p>We start by firing up our Rails application in console mode:<br />
<em>You must be in your Rails application directory to issue this command.</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">script<span style="color: #66cc66;">/</span>console</pre></td></tr></table></div>

<p>So now what? Well now you have command line access to your entire Rails application! For example, let&#8217;s say you have a class named Person (which would map to a people table in the database). You want a new instance of this class? Then issue the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">new</span></pre></td></tr></table></div>

<p>Perhaps you want to view a Person from the database:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>By default the results are displayed below each command and they are not very friendly, so let&#8217;s output them in YAML as it will be much more readable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span></pre></td></tr></table></div>

<p>&#8216;y&#8217; is like calling &#8216;puts p.to_yaml&#8217; on the resulting value that follows it. There is also a &#8216;p&#8217; shortcut that is the equivalent of issuing &#8216;puts&#8217; on the value.</p>
<p>So how does this help with determining attributes? At first glance you may think you have to look at the list of values returned in your YAML command and find what your looking for&#8230; no&#8230; it gets better. Remember that all Ruby objects inherit certain methods, watch this:</p>
<p>You want to get the methods associated with your object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span></pre></td></tr></table></div>

<p>You can also get the private, public, and protected methods using the following commands:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">public_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">private_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">protected_methods</span></pre></td></tr></table></div>

<p>So this class could return a huge YAML result which may not seem as efficient as scanning the schema in the database, and you&#8217;re right. So let&#8217;s get to what we are really looking for using &#8216;grep&#8217;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">grep</span> <span style="color:#006600; font-weight:bold;">/</span>method_name<span style="color:#006600; font-weight:bold;">/</span></pre></td></tr></table></div>

<p>This will return any method which contains <em>method_name</em>. You can write pass any regular expression you like into this command and really drill into your class methods, and for help putting those regular expressions together I really like using <a href="http://www.rubular.com/">Rubular</a> a web-based Ruby regular expression editor.</p>
<p>And what about the type of an object?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9966CC; font-weight:bold;">class</span></pre></td></tr></table></div>

<p>This is just a taste of what you can do with the console&#8230; I use the console everyday as I develop. I will post some more console tips in future posts!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

