<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7474356913652317146</id><updated>2012-02-10T04:10:43.968-08:00</updated><title type='text'>Ruby, Ruby with Rails</title><subtitle type='html'>RUBY, RAILS, RUBY ON RAILS, GENERAL INFORMATION RELATED TO WEB DEVELOPMENT AND RUBY WITH RAILS</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-2979946771785757202</id><published>2012-02-10T04:10:00.000-08:00</published><updated>2012-02-10T04:10:43.980-08:00</updated><title type='text'>XSS Protection in Rails 3</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;One of the places where web applications are vulnerable to XSS  attacks is when they display input from users. It is therefore important  to escape any user-generated input that is displayed and in Rails  applications this is usually done by making use of the &lt;code&gt;h&lt;/code&gt; method.&lt;br /&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&amp;lt;%=&amp;nbsp;h&amp;nbsp;comment.content&amp;nbsp;%&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div class="title"&gt;Using the h method to escape output.&lt;/div&gt;In Rails 3, however, output is escaped automatically so there’s no need to put the &lt;code&gt;h&lt;/code&gt; method in your views and in this episode we’ll show you how Rails 3 handles the escaping of output.&lt;br /&gt;To demonstrate output escaping we’re going to use a simple blogging  application written in Rails 3. In this app we have articles and each  article has a number of comments associated with it. To test how the  comment system copes with an attempt at cross-site scripting we’ll enter  &lt;code&gt;&amp;lt;script&amp;gt;alert('I steal cookies!')&amp;lt;/script&amp;gt;&lt;/code&gt; in each field in the comments form and submit our evil comment.&lt;br /&gt;&lt;div class="imageWrapper"&gt;   &lt;img alt="Entering an evil comment." height="527" src="http://asciicasts.com/system/photos/338/original/E204I01.png" width="815" /&gt; &lt;/div&gt;When we add the comment and it is displayed on the page we’ll see  that Rails 3 has automatically escaped the HTML tags in the fields that  we submitted. Let’s take a look at how Rails does this.&lt;br /&gt;&lt;div class="imageWrapper"&gt;   &lt;img alt="The HTML comments are automatically escaped." height="331" src="http://asciicasts.com/system/photos/339/original/E204I02.png" width="815" /&gt; &lt;/div&gt;The code that displays each comment is contained in a partial and if  you look at the code you’ll see that none of the output is escaped with &lt;code&gt;h&lt;/code&gt;.&lt;br /&gt;&lt;div class="codeFilePath"&gt;/app/views/comments/_comment.html.erb&lt;/div&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&amp;lt;div&amp;nbsp;&lt;span class="keyword"&gt;class&lt;/span&gt;=&lt;span class="string"&gt;"comment"&lt;/span&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&amp;lt;strong&amp;gt;&amp;lt;%=&amp;nbsp;link_to&amp;nbsp;comment.name,&amp;nbsp;comment.url&amp;nbsp;%&amp;gt;&amp;lt;/strong&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;&amp;lt;%=&amp;nbsp;comment.content&amp;nbsp;%&amp;gt;&amp;lt;/p&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;lt;/div&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;In Rails 2 this would have meant that the alert messages would have  been shown, but all of the output in the partial is being correctly  escaped by Rails 3, even when passed through helper methods such as  link_to so there’s no need to use the h method here.&lt;br /&gt;What happens, thought, if we’re transitioning an application from Rails 2 and have our output escaped with &lt;code&gt;h&lt;/code&gt;? We can find out by escaping the output in the partial above and reloading the page.&lt;br /&gt;&lt;div class="codeFilePath"&gt;/app/views/comments/_comment.html.erb&lt;/div&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&amp;lt;div&amp;nbsp;&lt;span class="keyword"&gt;class&lt;/span&gt;=&lt;span class="string"&gt;"comment"&lt;/span&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&amp;lt;strong&amp;gt;&amp;lt;%=&amp;nbsp;link_to&amp;nbsp;h(comment.name),&amp;nbsp;comment.url&amp;nbsp;%&amp;gt;&amp;lt;/strong&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;&amp;lt;%=&amp;nbsp;h&amp;nbsp;comment.content&amp;nbsp;%&amp;gt;&amp;lt;/p&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;lt;/div&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;When we reload the page we’ll see that it looks the same and the  output hasn’t been double-escaped. Rails is clever here; even if we use  the &lt;code&gt;h&lt;/code&gt; method it will escape the script tag only the once.&lt;br /&gt;It might seem that the h method does nothing in Rails 3 but that’s  not that case. It does still have a purpose and we’ll show you later  what that is. But before we do that we’ll look at a related feature.  While it’s great to have the output escaped automatically there might be  times when we want to display the raw content. If we trust the content  that the user enters, say they’re an administrative user, and we want to  display exactly what they enter then we can use the new &lt;code&gt;raw&lt;/code&gt; method to do that.&lt;br /&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&amp;lt;div&amp;nbsp;&lt;span class="keyword"&gt;class&lt;/span&gt;=&lt;span class="string"&gt;"comment"&lt;/span&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&amp;lt;strong&amp;gt;&amp;lt;%=&amp;nbsp;link_to&amp;nbsp;comment.name,&amp;nbsp;comment.url&amp;nbsp;%&amp;gt;&amp;lt;/strong&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;&amp;lt;%=&amp;nbsp;raw&amp;nbsp;comment.content&amp;nbsp;%&amp;gt;&amp;lt;/p&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;lt;/div&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;If we reload the page now the JavaScript we entered in the comment field will be executed.&lt;br /&gt;&lt;div class="imageWrapper"&gt;   &lt;img alt="Using the raw method means that the alert is shown." height="332" src="http://asciicasts.com/system/photos/340/original/E204I03.png" width="819" /&gt; &lt;/div&gt;So, in Rails 3 whenever we don’t want content to be HTML escaped we can use the &lt;code&gt;raw&lt;/code&gt;  method. But how does it work? Rails 3 seems to be pretty clever about  when to escape content and when not to. We’ll show you now how that  works.&lt;br /&gt;We’ll demonstrate escaping in the console, which in Rails 3 we can invoke with the &lt;code&gt;rails c&lt;/code&gt; command.&lt;br /&gt;&lt;pre class="terminal"&gt;$ rails c&lt;br /&gt;Loading development environment (Rails 3.0.0.beta)&lt;br /&gt;ruby-1.9.1-p378 &amp;gt;&lt;br /&gt;&lt;/pre&gt;Rails 3 has a concept of HTML-safe strings. This means that we can  check if any string is safe to output as HTML by calling the new method &lt;code&gt;html_safe?&lt;/code&gt; on it.&lt;br /&gt;&lt;pre class="terminal"&gt;&amp;gt; "foo".html_safe?&lt;br /&gt; =&amp;gt; false &lt;br /&gt;&lt;/pre&gt;We can mark a string as being HTML-safe by calling the &lt;code&gt;html_safe&lt;/code&gt; method on it.&lt;br /&gt;&lt;pre class="terminal"&gt;&amp;gt; safe = "safe".html_safe&lt;br /&gt; =&amp;gt; "safe" &lt;br /&gt;&amp;gt; safe.html_safe?&lt;br /&gt; =&amp;gt; true &lt;br /&gt;&lt;/pre&gt;No actual escaping goes on here. All that happens is that a boolean  property is set against a string to determine whether it should be  escaped before being output.&lt;br /&gt;So how does this apply in our view template? Well, Rails looks at  each piece of output and sees if it’s marked as HTML-safe. If it’s not  then it will be automatically escaped when it is output to the view. If  it is safe then it is passed through without being escaped. If we use  the &lt;code&gt;h&lt;/code&gt; method to escape a string it will perform the  escaping and mark the string as HTML-safe. This means that Rails 3 will  see that the string is safe and not escape it again.&lt;br /&gt;When the &lt;code&gt;raw&lt;/code&gt; method is used on a string it will be  marked as HTML-safe but not escaped, ensuring that the string’s content  is passed to the output without being changed.&lt;br /&gt;This is an important thing to understand when you’re using helper  methods. We’ll explain this by creating a helper method called &lt;code&gt;strong&lt;/code&gt; that wraps whatever is passed to it in &lt;code&gt;&amp;lt;strong&amp;gt;&lt;/code&gt; tags. We’ll use it in our template like this:&lt;br /&gt;&lt;div class="codeFilePath"&gt;/app/views/comments/_comment.html.erb&lt;/div&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&amp;lt;div&amp;nbsp;&lt;span class="keyword"&gt;class&lt;/span&gt;=&lt;span class="string"&gt;"comment"&lt;/span&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&amp;lt;%=&amp;nbsp;strong&amp;nbsp;link_to(comment.name,&amp;nbsp;comment.url)&amp;nbsp;%&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;&amp;lt;%=&amp;nbsp;raw&amp;nbsp;comment.content&amp;nbsp;%&amp;gt;&amp;lt;/p&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;lt;/div&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;We’ll create the &lt;code&gt;strong&lt;/code&gt; method in the &lt;code&gt;ApplicationHelper&lt;/code&gt;:&lt;br /&gt;&lt;div class="codeFilePath"&gt;/app/helpers/application_helper.rb&lt;/div&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&lt;span class="keyword"&gt;module&lt;/span&gt;&amp;nbsp;ApplicationHelper&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&lt;span class="keyword"&gt;def&lt;/span&gt;&amp;nbsp;strong(content)&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="string"&gt;"&amp;lt;strong&amp;gt;#{content}&amp;lt;/strong&amp;gt;"&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;When we reload the page, however, we’ll see that this hasn’t worked the way we wanted it to.&lt;br /&gt;&lt;div class="imageWrapper"&gt;   &lt;img alt="The strong tag in our helper method is escaped." height="552" src="http://asciicasts.com/system/photos/341/original/E204I04.png" width="815" /&gt; &lt;/div&gt;Rails 3’s automatic escaping has escaped the &lt;code&gt;&amp;lt;strong&amp;gt;&lt;/code&gt; tag here and this is because our helper method doesn’t create an HTML-safe string.&lt;br /&gt;There are two simple rules that need to be need to be followed when  working with helper methods that return HTML. Firstly, we need to ensure  that any strings returned are marked as HTML-safe.&lt;br /&gt;&lt;div class="codeFilePath"&gt;/app/helpers/application_helper.rb&lt;/div&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&lt;span class="keyword"&gt;module&lt;/span&gt;&amp;nbsp;ApplicationHelper&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&lt;span class="keyword"&gt;def&lt;/span&gt;&amp;nbsp;strong(content)&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="string"&gt;"&amp;lt;strong&amp;gt;#{content}&amp;lt;/strong&amp;gt;"&lt;/span&gt;.html_safe&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;This fixes the problem of the &lt;code&gt;&amp;lt;strong&amp;gt;&lt;/code&gt; tag being  escaped but by doing that the content between the tags will not be  escaped. We can solve this by escaping the content with &lt;code&gt;h&lt;/code&gt;:&lt;br /&gt;&lt;div class="codeFilePath"&gt;/app/helpers/application_helper.rb&lt;/div&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;div class="bar"&gt;&lt;/div&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&lt;span class="keyword"&gt;module&lt;/span&gt;&amp;nbsp;ApplicationHelper&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&lt;span class="keyword"&gt;def&lt;/span&gt;&amp;nbsp;strong(content)&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="string"&gt;"&amp;lt;strong&amp;gt;#{h(content)}&amp;lt;/strong&amp;gt;"&lt;/span&gt;.html_safe&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class=""&gt;&amp;nbsp;&amp;nbsp;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li class="alt"&gt;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;So as long as we remember to escape all input with &lt;code&gt;h&lt;/code&gt; and then mark the resulting string as  &lt;code&gt;html_safe&lt;/code&gt; it will be rendered properly. If we reload our comments page now we’ll see that the &lt;code&gt;&amp;lt;strong&amp;gt;&lt;/code&gt;  tag hasn’t been escaped but that the content of the second comment,  which includes the potentially dangerous JavaScript is escaped.&lt;br /&gt;&lt;div class="imageWrapper"&gt;   &lt;img alt="The helper method is now escaped correctly." height="552" src="http://asciicasts.com/system/photos/342/original/E204I05.png" width="815" /&gt; &lt;/div&gt;That’s it for this episode. The automatic escaping is a welcome  addition to Rails 3’s views and it removes the need to remember to  escape every piece of output with &lt;code&gt;h&lt;/code&gt;, this reducing the changes of your application being a victim of cross-site scripting.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-2979946771785757202?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/2979946771785757202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/xss-protection-in-rails-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/2979946771785757202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/2979946771785757202'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/xss-protection-in-rails-3.html' title='XSS Protection in Rails 3'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-298633032843912392</id><published>2012-02-10T04:08:00.000-08:00</published><updated>2012-02-10T04:08:43.490-08:00</updated><title type='text'>Cross-site Scripting</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;&lt;div id="otherFormats"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;Cross-site scripting is another common security issue to consider  when developing web applications. It happens when you allow the users of  your site to enter HTML or JavaScript directly. In the website below  we’re not escaping the input that’s entered in the comments textbox,  which leaves our site vulnerable.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-fTL9H4nEuAU/TzUBm39rLrI/AAAAAAAAAAQ/096U4ih9BCM/s1600/E027I01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;img border="0" height="106" src="http://1.bp.blogspot.com/-fTL9H4nEuAU/TzUBm39rLrI/AAAAAAAAAAQ/096U4ih9BCM/s320/E027I01.png" width="320" /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;f we enter JavaScript into the comment box, surrounded by &amp;lt;script&amp;gt;  tags, that script will be executed when the page reloads and every time  it it viewed afterwards. For example if we entered  &amp;lt;script&amp;gt;alert(’hello’)&amp;lt;/script&amp;gt; into the comments box and  clicked the button then every subsquent time the page is viewed the user  would see an alert box.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-PMy96yv48Nk/TzUGHSu3IwI/AAAAAAAAAAY/BibbBIw1Qg0/s1600/E027I02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;img border="0" height="116" src="http://1.bp.blogspot.com/-PMy96yv48Nk/TzUGHSu3IwI/AAAAAAAAAAY/BibbBIw1Qg0/s320/E027I02.png" width="320" /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;If we look at the source of the page we can see the JavaScript embedded in it.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;lt;h2&amp;gt;Comments&amp;lt;/h2&amp;gt;&lt;br /&gt;  &amp;lt;p&amp;gt;I'll start on this now - Paul&amp;lt;/p&amp;gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&amp;lt;script&amp;gt;alert('hello!');&amp;lt;/script&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;&amp;lt;hr/&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;Causing an alert box to be shown on a page is annoying, but cross-site &lt;br /&gt;scripting can be used for much more malicious purposes. For example it &lt;br /&gt;could be used to read the cookies for the site’s other users. We’ll just&lt;br /&gt; alert the cookie, but it could just as easily be sent to a remote &lt;br /&gt;server where the session id information in the cookie could be used to &lt;br /&gt;hijack another user’s session&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-q2s_7etKoos/TzUHD3SXxsI/AAAAAAAAAAg/SyYoixEr7lM/s1600/E027I03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;img border="0" height="150" src="http://1.bp.blogspot.com/-q2s_7etKoos/TzUHD3SXxsI/AAAAAAAAAAg/SyYoixEr7lM/s320/E027I03.png" width="320" /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="terminal"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="terminal"&gt;&lt;h3&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Stopping The Attacks&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;To stop these attacks you need to escape any user input before you &lt;br /&gt;display it on the screen.  We’re currently taking the comment’s content &lt;br /&gt;directly from the database and outputting it into the HTML stream. Rails&lt;br /&gt; provides a method simply called h to escape the content before it is &lt;br /&gt;output.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;/div&gt;&lt;li class="alt"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;lt;%&amp;nbsp;&lt;span class="variable"&gt;@task&lt;/span&gt;.comments.&lt;span class="keyword"&gt;each&lt;/span&gt;&amp;nbsp;&lt;span class="keyword"&gt;do&lt;/span&gt;&amp;nbsp;|comment|&amp;nbsp;%&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class=""&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;lt;p&amp;gt;&amp;lt;%=&amp;nbsp;h(comment.content)&amp;nbsp;%&amp;gt;&amp;lt;/p&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class="alt"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;lt;%&amp;nbsp;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;%&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class="alt"&gt;&lt;b style="font-family: Arial, Helvetica, sans-serif;"&gt;Now, when we reload the page we can see that the script is no longer executed. The &lt;code&gt;h&lt;/code&gt; command escapes the angle brackets so that the comments entered by the user are displayed safely.&lt;/b&gt;&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;Rails also provides a &lt;code&gt;sanitize&lt;/code&gt; method which allows &lt;br /&gt;certain tags to be included via a white list. To be on the safe side and&lt;br /&gt; escape any HTML that may be entered by a user it is safer to stick to &lt;code&gt;h&lt;/code&gt;.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;h3&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;An Alternative Approach&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;Instead of escaping the user’s input when sending it out to the &lt;br /&gt;browser we could escape it when we store it in the database. The &lt;code&gt;h&lt;/code&gt; method isn’t available in a controller so instead we’ll use &lt;code&gt;CGI::escapeHTML()&lt;/code&gt;.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="dp-highlighter nogutter"&gt;&lt;ol class="dp-rb" start="1"&gt;&lt;li class="alt"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="keyword"&gt;def&lt;/span&gt;&amp;nbsp;create&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class=""&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&lt;span class="variable"&gt;@task&lt;/span&gt;&amp;nbsp;=&amp;nbsp;Task.find(params[&lt;span class="symbol"&gt;:task_id&lt;/span&gt;])&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class="alt"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&lt;span class="variable"&gt;@comment&lt;/span&gt;&amp;nbsp;=&amp;nbsp;&lt;span class="variable"&gt;@task&lt;/span&gt;.comments.&lt;span class="keyword"&gt;new&lt;/span&gt;(params[&lt;span class="symbol"&gt;:comment&lt;/span&gt;])&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class=""&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&lt;span class="variable"&gt;@comment&lt;/span&gt;.content&amp;nbsp;=&amp;nbsp;CGI:&lt;span class="symbol"&gt;:escapeHTML&lt;/span&gt;(params[&lt;span class="symbol"&gt;:comment&lt;/span&gt;][&lt;span class="symbol"&gt;:content&lt;/span&gt;])&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class="alt"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&lt;span class="variable"&gt;@comment&lt;/span&gt;.save&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class=""&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;redirect_to&amp;nbsp;task_path(&lt;span class="variable"&gt;@task&lt;/span&gt;)&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li class="alt"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="keyword"&gt;end&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;div class="title"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;Escaping the comment’s HTML before it is stored in the database.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;Storing escaped HTML in the database is fine as long as you only ever&lt;br /&gt; want to display that data in a browser. If you think you might need it &lt;br /&gt;in its unescaped form as well then it’s better to escape the output with&lt;br /&gt; &lt;code&gt;h&lt;/code&gt;.  &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ol class="footnotes"&gt;&lt;li id="fn1"&gt;&lt;a href="http://en.wikipedia.org/wiki/Session_hijacking"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;http://en.wikipedia.org/wiki/Session_hijacking&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-298633032843912392?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/298633032843912392/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/cross-site-scripting.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/298633032843912392'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/298633032843912392'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/cross-site-scripting.html' title='Cross-site Scripting'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-fTL9H4nEuAU/TzUBm39rLrI/AAAAAAAAAAQ/096U4ih9BCM/s72-c/E027I01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-5196767065247554261</id><published>2012-02-10T02:51:00.001-08:00</published><updated>2012-02-10T02:51:47.676-08:00</updated><title type='text'>How to identify that object belong to which class?</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):001:0&amp;gt; str = Class.new(String)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; #&amp;lt;Class:0xb5cfc3c0&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):002:0&amp;gt; str.class&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; Class&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):003:0&amp;gt; str.parent&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; Object&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):004:0&amp;gt; str.class.superclass&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; Module&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):005:0&amp;gt; str.superclass&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; String&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;my_str = str.new("hello")&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; "hello"&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):008:0&amp;gt; my_str.class&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; #&amp;lt;Class:0xb5e66080&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):009:0&amp;gt; my_str.parent&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;NoMethodError: undefined method `parent' for "hello":#&amp;lt;Class:0xb5e66080&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;from (irb):9&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):010:0&amp;gt; my_str.class.superclass&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; String&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):011:0&amp;gt; my_str.class.parent&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; Object&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;irb(main):012:0&amp;gt; my_str.class.superclass.parent&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;=&amp;gt; Object&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #20124d; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-5196767065247554261?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/5196767065247554261/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/how-to-identify-that-object-belong-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/5196767065247554261'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/5196767065247554261'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/how-to-identify-that-object-belong-to.html' title='How to identify that object belong to which class?'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-2488018158663055547</id><published>2012-02-10T01:38:00.000-08:00</published><updated>2012-02-10T01:55:25.938-08:00</updated><title type='text'>Class and Instance Methods in Ruby</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;In ruby, classes are also objects, so the methods you define as &lt;em&gt;class methods&lt;/em&gt; only exist in the object that defined them (the class) and nowhere else. If you define &lt;em&gt;class_method&lt;/em&gt; in a class named &lt;em&gt;Test&lt;/em&gt;, &lt;em&gt;class_method&lt;/em&gt; will only live into the object &lt;em&gt;Test&lt;/em&gt;, which is an instance of a built-in ruby class named &lt;em&gt;Class&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class="comment-copy"&gt;&lt;span class="Apple-style-span" style="color: #660000; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;class methods are singleton methods of instances of Class&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="comment-copy"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;But in general u will feel that&amp;nbsp;Class methods are methods that are called on a class and instance  methods are methods that are called on an instance of a class. &lt;br /&gt;&lt;br /&gt;Every instances that you create contain a variable that points to  their corresponding class. Say you create a new instance of class Test  and that you name this instance T (T = Test.new). T will contain every  instance variables/methods that have been defined as such in the class  as well as a reference to the object Test, which will contain the  variables that need to be shared among every instances of this class  (class variables) as well as methods that will be local to this object  (class methods). That’s the reason why ruby complain when you write  something like T.class_method. It complains because class_method  just doesn’t reside in T, it resides in Test.&lt;br /&gt;You have 2 ways for defining a class method.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1) You can use the name of the class directly&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="syntaxhighlighter  ruby" id="highlighter_645290"&gt;&lt;table border="0" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="container"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;code class="ruby keyword"&gt;&amp;nbsp; &amp;nbsp; &lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt; &lt;code class="ruby plain"&gt;My&amp;nbsp;&lt;/code&gt;&lt;code class="ruby comments"&gt;#My is now an instance of a built-in class named Class&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;def&lt;/code&gt; &lt;code class="ruby plain"&gt;My.class_method&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/code&gt;&lt;code class="ruby string"&gt;"It's a class method."&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;end&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;strong&gt;2) You can use the self variable, which is always pointing to the current object&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="syntaxhighlighter  ruby" id="highlighter_677993"&gt;&lt;table border="0" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="container"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;code class="ruby keyword"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt; &lt;code class="ruby plain"&gt;My&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;def&lt;/code&gt; &lt;code class="ruby keyword"&gt;self&lt;/code&gt;&lt;code class="ruby plain"&gt;.class_method&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby string"&gt;"It's a class method."&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;end&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;end&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;3 &amp;nbsp;&lt;/span&gt;I&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;t&lt;/span&gt; consists of building a new anonymous class only for the current object (self). To create the anonymous class, we use the &amp;lt;&amp;lt; notation.&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;/code&gt;&lt;br /&gt;&lt;pre&gt;&lt;code class="ruby keyword"&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;    &lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt; test&lt;br /&gt;            &lt;span class="keywords"&gt;class&lt;/span&gt; &amp;lt;&amp;lt; &lt;span class="keywords"&gt;self&lt;/span&gt;&lt;br /&gt;                &lt;span class="keywords"&gt;def&lt;/span&gt; bar&lt;br /&gt;                    puts &lt;span class="string"&gt;'It's a class Method'&lt;/span&gt;&lt;br /&gt;                &lt;span class="keywords"&gt;end&lt;/span&gt;&lt;br /&gt;           &lt;span class="keywords"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;span class="keywords"&gt;       end&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby keyword"&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby keyword"&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;4&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="keywords"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;     &lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt; Test;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby keyword"&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;         &lt;span class="keywords"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;span class="keywords"&gt;          def&lt;/span&gt; Test.bar&lt;br /&gt;            puts 'It's a class Method'&lt;br /&gt;&lt;br /&gt;&lt;span class="keywords"&gt;          end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;5 Way to call class method&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;  &lt;span class="Apple-style-span" style="color: #351c75; font-size: large;"&gt;&lt;b&gt; class_name.method_name&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;      like here&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;                 &lt;b&gt;&lt;span class="Apple-style-span" style="color: #351c75;"&gt; my.class_method&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;      or&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;               &lt;b&gt;&lt;span class="Apple-style-span" style="color: #351c75;"&gt;   test.bar&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;Once you understand that classes are objects, this use of the self variable to define a class method finally makes sense.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;/div&gt;Remember what we have to do to add a new instance method in a class :&lt;br /&gt;&lt;div&gt;&lt;div class="syntaxhighlighter  ruby" id="highlighter_978111"&gt;&lt;table border="0" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number6 index5 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number7 index6 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number8 index7 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number9 index8 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number10 index9 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number11 index10 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number12 index11 alt1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="line number13 index12 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="container"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby keyword"&gt;class&lt;/code&gt; &lt;code class="ruby plain"&gt;Test&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;def&lt;/code&gt; &lt;code class="ruby plain"&gt;hello&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby string"&gt;"It's an instance method"&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby string"&gt;"It'saccessible in every single instance of class Test"&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;end&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number6 index5 alt1"&gt;&lt;code class="ruby keyword"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number7 index6 alt2"&gt;&lt;/div&gt;&lt;div class="line number8 index7 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby plain"&gt;T = Test.&lt;/code&gt;&lt;code class="ruby keyword"&gt;new&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number9 index8 alt2"&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;When you look at it, it's exactly the same trick that we previously  used to add a new class method. The only difference is that  instead of  adding a method to an instance of class &lt;em&gt;Class&lt;/em&gt;, we added a method to an instance of class &lt;em&gt;Test&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The value of self&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;Not too surprinsingly, when you are inside a class method, the value  of self refers to the object that holds the class structure (the  instance of class Class). This means that :&lt;br /&gt;&lt;div&gt;&lt;div class="syntaxhighlighter  ruby" id="highlighter_486355"&gt;&lt;table border="0" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="container"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby keyword"&gt;class&lt;/code&gt; &lt;code class="ruby plain"&gt;Test&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;def&lt;/code&gt; &lt;code class="ruby keyword"&gt;self&lt;/code&gt;&lt;code class="ruby plain"&gt;.class_method&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;self&lt;/code&gt;&lt;code class="ruby plain"&gt;.T&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;end&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;is equivalent to :&lt;br /&gt;&lt;div&gt;&lt;div class="syntaxhighlighter  ruby" id="highlighter_294260"&gt;&lt;table border="0" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="container"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby keyword"&gt;class&lt;/code&gt; &lt;code class="ruby plain"&gt;Test&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;def&lt;/code&gt; &lt;code class="ruby keyword"&gt;self&lt;/code&gt;&lt;code class="ruby plain"&gt;.class_method&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby plain"&gt;Test.x&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number4 index3 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="ruby spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="ruby keyword"&gt;end&lt;/code&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number5 index4 alt2"&gt;&lt;code class="ruby keyword"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;When you are inside an instance method, the value of self still  refers to the current object. This time however, the current object is  an instance of class Test, not an instance of class &lt;em&gt;Class&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;other uses of class methods in Rails are validations and associations  in ActiveRecord and before/after/around filters in ActionPack. The way  this works is something like this (simplified for clarity):&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="keywords"&gt;module&lt;/span&gt; ActiveRecord&lt;br /&gt;  &lt;span class="keywords"&gt;class&lt;/span&gt; Base&lt;br /&gt;    &lt;span class="keywords"&gt;def&lt;/span&gt; &lt;span class="keywords"&gt;self&lt;/span&gt;.validates_presence_of&lt;span class="brackets"&gt;(&lt;/span&gt;...&lt;span class="brackets"&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class="comment"&gt;# make sure present&lt;/span&gt;&lt;br /&gt;    &lt;span class="keywords"&gt;end&lt;/span&gt;&lt;br /&gt;  &lt;span class="keywords"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;span class="keywords"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="keywords"&gt;class&lt;/span&gt; Foo &amp;lt; ActiveRecord::Base&lt;br /&gt;  validates_presence_of&lt;span class="symbol"&gt; :bar&lt;/span&gt;&lt;br /&gt;&lt;span class="keywords"&gt;end&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="keywords"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;When you say validates_presence_of, the class method in AR::Base is what gets called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So what are some examples uses of instance methods in Rails, to give  you a better idea? Ever do a find in a destroy action and then call  destroy on the found instance? destroy is an instance method.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="keywords"&gt;class&lt;/span&gt; FoosController &amp;lt; ActionController&lt;br /&gt;  &lt;span class="keywords"&gt;def&lt;/span&gt; destroy&lt;br /&gt;    foo = Foo.find&lt;span class="brackets"&gt;(&lt;/span&gt;params&lt;span class="symbol"&gt;[:id&lt;/span&gt;]&lt;span class="brackets"&gt;)&lt;/span&gt;&lt;br /&gt;    foo.destroy&lt;br /&gt;    redirect_to foos_url&lt;br /&gt;  &lt;span class="keywords"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;span class="keywords"&gt;end&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="keywords"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;So are save and update_attributes, which you have definitely used before if you’ve done any Rails.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;foo = Foo.new&lt;span class="brackets"&gt;(&lt;/span&gt;:title =&amp;gt; &lt;span class="string"&gt;'Bar'&lt;/span&gt;&lt;span class="brackets"&gt;)&lt;/span&gt;&lt;br /&gt;foo.save &lt;span class="comment"&gt;# is an instance method&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-2488018158663055547?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/2488018158663055547/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/class-and-instance-methods-in-ruby.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/2488018158663055547'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/2488018158663055547'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/class-and-instance-methods-in-ruby.html' title='Class and Instance Methods in Ruby'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-5971227754650578925</id><published>2012-02-08T23:27:00.000-08:00</published><updated>2012-02-08T23:59:33.705-08:00</updated><title type='text'>ruby shortcut and format specifier</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;NOTE: Some of these seem only to work for DateTime (e.g. %L, %N)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%a - The abbreviated weekday name (“Sun”)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%A - The  full  weekday  name (“Sunday”)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%b - The abbreviated month name (“Jan”)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%B - The  full  month  name (“January”)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%c - The preferred local date and time representation&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%C - Century (20 in 2009)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%d - Day of the month (01..31)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%D - Date (%m/%d/%y)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%e - Day of the month, blank-padded ( 1..31)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%F - Equivalent to %Y-%m-%d (the ISO 8601 date format)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%h - Equivalent to %b&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%H - Hour of the day, 24-hour clock (00..23)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%I - Hour of the day, 12-hour clock (01..12)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%j - Day of the year (001..366)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%k - hour, 24-hour clock, blank-padded ( 0..23)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%l - hour, 12-hour clock, blank-padded ( 0..12)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%L - Millisecond of the second (000..999)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%m - Month of the year (01..12)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%M - Minute of the hour (00..59)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%n - Newline (n)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%N - Fractional seconds digits, default is 9 digits (nanosecond)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt; %3N  millisecond (3 digits)&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt; %6N  microsecond (6 digits)&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt; %9N  nanosecond (9 digits)&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%p - Meridian indicator (“AM”  or  “PM”)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%P - Meridian indicator (“am”  or  “pm”)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%r - time, 12-hour (same as %I:%M:%S %p)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%R - time, 24-hour (%H:%M)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%s - Number of seconds since 1970-01-01 00:00:00 UTC.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%S - Second of the minute (00..60)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%t - Tab character (t)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%T - time, 24-hour (%H:%M:%S)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%u - Day of the week as a decimal, Monday being 1. (1..7)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%U - Week  number  of the current year, starting with the first Sunday as the first day of the first week (00..53)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%v - VMS date (%e-%b-%Y)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%V - Week number of year according to ISO 8601 (01..53)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%W - Week  number  of the current year, starting with the first Monday as the first day of the first week (00..53)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%w - Day of the week (Sunday is 0, 0..6)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%x - Preferred representation for the date alone, no time&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%X - Preferred representation for the time alone, no date&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%y - Year without a century (00..99)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%Y - Year with century&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%z - Time zone as  hour offset from UTC (e.g. +0900)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%Z - Time zone name&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;%% - Literal “%” character&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b style="background-color: white;"&gt;&lt;span class="ident"&gt;t&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;now&lt;/span&gt;                        &lt;span class="comment"&gt;#=&amp;gt; 2007-11-19 08:37:48 -0600&lt;/span&gt;&lt;br /&gt;&lt;span class="ident"&gt;t&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;strftime&lt;/span&gt;&lt;span class="punct"&gt;("&lt;/span&gt;&lt;span class="string"&gt;Printed on %m/%d/%Y&lt;/span&gt;&lt;span class="punct"&gt;")&lt;/span&gt;   &lt;span class="comment"&gt;#=&amp;gt; "Printed on 11/19/2007"&lt;/span&gt;&lt;br /&gt;&lt;span class="ident"&gt;t&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;strftime&lt;/span&gt;&lt;span class="punct"&gt;("&lt;/span&gt;&lt;span class="string"&gt;at %I:%M%p&lt;/span&gt;&lt;span class="punct"&gt;")&lt;/span&gt;            &lt;span class="comment"&gt;#=&amp;gt; "at 08:37AM"&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-5971227754650578925?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/5971227754650578925/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/ruby-shortcut-and-format-specifier.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/5971227754650578925'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/5971227754650578925'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/ruby-shortcut-and-format-specifier.html' title='ruby shortcut and format specifier'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-4281765737597664997</id><published>2012-02-07T23:44:00.000-08:00</published><updated>2012-02-08T00:42:42.483-08:00</updated><title type='text'>WORKING WITH YIELD AND  content_for METHOD</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;&lt;pre class="ruby"&gt;&lt;span class="punct"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;"&gt;Within the context of a layout, &lt;/span&gt;&lt;tt style="white-space: normal;"&gt;yield&lt;/tt&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;"&gt; identifies a section where content from the view should be inserted.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="punct"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="punct" style="color: #e06666;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="Apple-style-span" style="color: #674ea7; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;html&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;head&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;title&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;&lt;span class="string"&gt;Home Page&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;  &amp;lt;table width="100%"&amp;gt;&lt;br /&gt;    &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;      &amp;lt;%= yield %&amp;gt;&lt;br /&gt;    &amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;      &amp;lt;%= yield :sidebar %&amp;gt;&lt;br /&gt;    &amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;      &amp;lt;/table&amp;gt;&lt;br /&gt;  &amp;lt;/body&amp;gt;&lt;br /&gt; &amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt;The first &lt;span class="code"&gt;yield &lt;/span&gt;will be replaced with HOME view when rendered.  The second &lt;span class="code"&gt;yield&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt; is a different: we are passing it a symbol, saying he wants to yield &lt;span class="code"&gt;:sidebar&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt; at that point in time&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt;Now The &lt;tt&gt;content_for&lt;/tt&gt; method allows you to insert content into a named &lt;tt&gt;yield&lt;/tt&gt; block&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt; in your layout&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="ruby"&gt;&lt;span class="string"&gt;&lt;h4 id="using-the-content_for-method"&gt;&lt;div class="line number1 index0 alt2"&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;code class="ruby script"&gt;&amp;lt;%&lt;/code&gt; &lt;code class="ruby plain"&gt;content_for &lt;/code&gt;&lt;code class="ruby color2"&gt;:sidebar&lt;/code&gt; &lt;code class="ruby keyword"&gt;do&lt;/code&gt; &lt;code class="ruby script"&gt;%&amp;gt;&lt;/code&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="line number2 index1 alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;code class="htmlscript spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code class="htmlscript plain"&gt;&amp;lt;&lt;/code&gt;&lt;code class="htmlscript keyword"&gt;title&lt;/code&gt;&lt;code class="htmlscript plain"&gt;&amp;gt;sidebar&amp;lt;/&lt;/code&gt;&lt;code class="htmlscript keyword"&gt;title&lt;/code&gt;&lt;code class="ruby plain"&gt;&amp;gt;&lt;/code&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;code class="ruby script"&gt;&amp;lt;%&lt;/code&gt; &lt;code class="ruby keyword"&gt;end&lt;/code&gt; &lt;code class="ruby script"&gt;%&amp;gt;&lt;/code&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;code class="ruby script"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div class="line number3 index2 alt2"&gt;&lt;code class="ruby script"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;/h4&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-4281765737597664997?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/4281765737597664997/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/working-with-yield-and-contentfor.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/4281765737597664997'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/4281765737597664997'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/working-with-yield-and-contentfor.html' title='WORKING WITH YIELD AND  content_for METHOD'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-3519297981839861201</id><published>2012-02-07T23:29:00.000-08:00</published><updated>2012-02-08T00:46:27.202-08:00</updated><title type='text'>Blocking default design present in application layout</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;This is the way from where you are calling your default sidebar in application.html.erb&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;lt;div id="define_according_to_your_stylesheet" class="&amp;lt;%=&amp;nbsp;sidebar_disabled?&amp;nbsp;%&amp;gt;"&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&amp;lt;!-- START Sidebar --&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;%= yield :sidebar %&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;%= app_screen_tip -%&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&amp;lt;!-- END Sidebar --&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/div&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here&lt;span class="Apple-style-span" style="color: #351c75;"&gt;&lt;b&gt;&amp;nbsp;&amp;lt;%=sidebar_disabled?&amp;nbsp;%&lt;/b&gt;&amp;gt;&lt;/span&gt; is calling method present in application helper&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;define a method in application helper which is loaded automatically when your application loaded&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;def sidebar_disabled?&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; current_page = "#{controller.controller_name}.#{controller.action_name}"&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; current_controller = controller.controller_name&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; pages = %w(&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; home.index&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;#comment [home(its your controller name).index(its your action)]&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;)&amp;nbsp; &amp;nbsp; return pages.include?(current_controller) || pages.include?(current_page)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif; font-size: large;"&gt;&lt;b&gt;&amp;nbsp; end&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;by this way you can block anything which loaded by default when application loaded&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-3519297981839861201?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/3519297981839861201/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/blocking-default-design-present-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/3519297981839861201'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/3519297981839861201'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/blocking-default-design-present-in.html' title='Blocking default design present in application layout'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-8147848870620630033</id><published>2012-02-06T04:41:00.000-08:00</published><updated>2012-02-06T04:41:20.740-08:00</updated><title type='text'>Download YouTube Videos from Ubuntu 11.10</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;&lt;h2 style="background-color: white; color: #020866; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 20px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: -webkit-auto;"&gt;YouTube&amp;nbsp;&lt;a class="zem_slink" href="http://en.wikipedia.org/wiki/VideoDownloader" rel="wikipedia" style="color: #1779be; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;" title="VideoDownloader"&gt;Video Downloader&lt;/a&gt;&amp;nbsp;in&amp;nbsp;&lt;a class="zem_slink" href="http://www.ubuntu.com/" rel="homepage" style="color: #1779be; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;" title="Ubuntu (operating system)"&gt;Ubuntu&lt;/a&gt;&amp;nbsp;11.10&lt;/h2&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;You can use various ways to download a&amp;nbsp;youtube&amp;nbsp;video into your ubuntu 11.1o. Using a yoututbe client, a&amp;nbsp;&lt;a class="zem_slink" href="http://en.wikipedia.org/wiki/Command-line_interface" rel="wikipedia" style="color: #1779be; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;" title="Command-line interface"&gt;command line&lt;/a&gt;utility, a&amp;nbsp;&lt;span class="IL_AD" id="IL_AD11" style="background-attachment: scroll !important; background-clip: initial !important; background-color: transparent !important; background-image: none !important; background-origin: initial !important; background-position: 0% 50%; background-repeat: repeat repeat !important; border-bottom-color: rgb(0, 153, 0) !important; border-bottom-style: solid !important; border-bottom-width: 1px !important; color: rgb(0, 153, 0) !important; cursor: pointer !important; display: inline !important; float: none !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static; text-decoration: underline !important;"&gt;web browser&lt;/span&gt;&amp;nbsp;plugin are available for&amp;nbsp;&lt;span class="IL_AD" id="IL_AD8" style="background-attachment: scroll !important; background-clip: initial !important; background-color: transparent !important; background-image: none !important; background-origin: initial !important; background-position: 0% 50%; background-repeat: repeat repeat !important; border-bottom-color: rgb(0, 153, 0) !important; border-bottom-style: solid !important; border-bottom-width: 1px !important; color: rgb(0, 153, 0) !important; cursor: pointer !important; display: inline !important; float: none !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static; text-decoration: underline !important;"&gt;downloading&lt;/span&gt;&amp;nbsp;videos from&amp;nbsp;youtube.&lt;/div&gt;&lt;h3 style="background-color: white; color: #4603a5; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: -webkit-auto;"&gt;&lt;strong style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;MiniTube&lt;/strong&gt;&lt;/h3&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;Minitube is the best&amp;nbsp;youtube&amp;nbsp;client in ubuntu 11.1o. You can watch&amp;nbsp;Youtube&amp;nbsp;Videos right from your desktop. You can easly download&amp;nbsp;youtube&amp;nbsp;videos into ubuntu 11.10 using minitube. It has a very simple and easy&amp;nbsp;&lt;a class="zem_slink" href="http://en.wikipedia.org/wiki/Graphical_user_interface" rel="wikipedia" style="color: #1779be; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;" title="Graphical user interface"&gt;GUI&lt;/a&gt;. You can configure the video size, related videos etc. You can easily search using keywords or a&amp;nbsp;youtube&amp;nbsp;channel name.&amp;nbsp;&lt;span class="IL_AD" id="IL_AD10" style="background-attachment: scroll !important; background-clip: initial !important; background-color: transparent !important; background-image: none !important; background-origin: initial !important; background-position: 0% 50%; background-repeat: repeat repeat !important; border-bottom-color: rgb(0, 153, 0) !important; border-bottom-style: solid !important; border-bottom-width: 1px !important; color: rgb(0, 153, 0) !important; cursor: pointer !important; display: inline !important; float: none !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static; text-decoration: underline !important;"&gt;The project&lt;/span&gt;&amp;nbsp;is hosted in gitorius and is released as GPL.&lt;/div&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;&lt;a href="http://smashingweb.ge6.org/wp-content/uploads/2011/10/Minitube.png" style="color: #1779be; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;"&gt;&lt;img alt="" class="aligncenter size-medium wp-image-1615" height="210" src="http://smashingweb.ge6.org/wp-content/uploads/2011/10/Minitube-300x210.png" style="border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; display: block; margin-bottom: 10px; margin-left: auto; margin-right: auto; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="Minitube" width="300" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h3 style="background-color: white; color: #4603a5; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: -webkit-auto;"&gt;Install Minitube, The Youtube Downloader in Ubuntu 11.10&lt;/h3&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;Minitube is available in&amp;nbsp;&lt;span class="IL_AD" id="IL_AD6" style="background-attachment: scroll !important; background-clip: initial !important; background-color: transparent !important; background-image: none !important; background-origin: initial !important; background-position: 0% 50%; background-repeat: repeat repeat !important; border-bottom-color: rgb(0, 153, 0) !important; border-bottom-style: solid !important; border-bottom-width: 1px !important; color: rgb(0, 153, 0) !important; cursor: pointer !important; display: inline !important; float: none !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static; text-decoration: underline !important;"&gt;official&lt;/span&gt;&amp;nbsp;package repository. To install minitube just type the command in command line.&lt;/div&gt;&lt;pre style="background-attachment: scroll; background-clip: initial; background-color: white; background-image: none; background-origin: initial; background-position: 0px 0px; background-repeat: repeat repeat; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(221, 221, 221); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; clear: both; color: #7a1d20; font-family: Consolas, 'Andale Mono', Monaco, Courier, 'Courier New', Verdana, sans-serif; font-size: 13px; margin-bottom: 5px; margin-left: 5px; margin-right: 5px; margin-top: 5px; overflow-x: auto; overflow-y: auto; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px; text-align: -webkit-auto;"&gt;sudo apt-get install minitube&lt;/pre&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;you can install using&amp;nbsp;&lt;a class="zem_slink" href="https://wiki.ubuntu.com/SoftwareCenter" rel="homepage" style="color: #1779be; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;" title="Ubuntu Software Center"&gt;Ubuntu Software Centre&lt;/a&gt;&amp;nbsp;also. Just search for minitube in the software centre and click install.&lt;/div&gt;&lt;h4 style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 16px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: -webkit-auto;"&gt;&lt;a class="zem_slink" href="http://en.wikipedia.org/wiki/Uploading_and_downloading" rel="wikipedia" style="color: #1779be; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none;" title="Uploading and downloading"&gt;Download&lt;/a&gt;&amp;nbsp;videos using MiniTube in Ubuntu 11.1o&lt;/h4&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;To download a video just play the video in minitube. The go Video &amp;gt; Download in the menu. The videos will be saved to /home/username/Videos.&lt;/div&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;&lt;strong style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&amp;nbsp;Download&amp;nbsp;Youtube&amp;nbsp;Videos Using Terminal Utility&lt;/strong&gt;&lt;/div&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;You can download&amp;nbsp;youtube&amp;nbsp;videos by using the command line. To&amp;nbsp;download videos&amp;nbsp;from command line you can use the command&amp;nbsp;youtube-dl.&amp;nbsp; Install install a utility&amp;nbsp;&amp;nbsp;youtube-dl. Open terminal and type&lt;/div&gt;&lt;pre style="background-attachment: scroll; background-clip: initial; background-color: white; background-image: none; background-origin: initial; background-position: 0px 0px; background-repeat: repeat repeat; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(221, 221, 221); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; clear: both; color: #7a1d20; font-family: Consolas, 'Andale Mono', Monaco, Courier, 'Courier New', Verdana, sans-serif; font-size: 13px; margin-bottom: 5px; margin-left: 5px; margin-right: 5px; margin-top: 5px; overflow-x: auto; overflow-y: auto; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px; text-align: -webkit-auto;"&gt;sudo apt-get install youtube-dl&lt;/pre&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;Now, open the&amp;nbsp;youtube&amp;nbsp;video in the browser and copy the URL from the address bar.&lt;/div&gt;&lt;div style="background-color: white; font-family: Arial, Tahoma, 'Century gothic', sans-serif; font-size: 13px; line-height: 22px; margin-top: 5px; padding-bottom: 5px; padding-left: 0px; padding-right: 0px; padding-top: 5px; text-align: -webkit-auto;"&gt;Then execute the following command -&lt;/div&gt;&lt;pre style="background-attachment: scroll; background-clip: initial; background-color: white; background-image: none; background-origin: initial; background-position: 0px 0px; background-repeat: repeat repeat; border-bottom-color: rgb(221, 221, 221); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(221, 221, 221); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; clear: both; color: #7a1d20; font-family: Consolas, 'Andale Mono', Monaco, Courier, 'Courier New', Verdana, sans-serif; font-size: 13px; margin-bottom: 5px; margin-left: 5px; margin-right: 5px; margin-top: 5px; overflow-x: auto; overflow-y: auto; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px; text-align: -webkit-auto;"&gt;youtube-dl "http://youtube.com/?v=VideoID"&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-8147848870620630033?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/8147848870620630033/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/download-youtube-videos-from-ubuntu.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/8147848870620630033'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/8147848870620630033'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/download-youtube-videos-from-ubuntu.html' title='Download YouTube Videos from Ubuntu 11.10'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-1326485023731518221</id><published>2012-02-05T22:12:00.000-08:00</published><updated>2012-02-08T00:48:19.126-08:00</updated><title type='text'>IE9 DELETES stuff</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;THANKS TO:-&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; http://techno-weenie.net/2011/8/19/ie9-deletes-stuff &amp;nbsp; (FROM WHERE I GOT THIS INFORMATION)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Using more of the HTTP methods lets us keep the URLs cleaner.  Web browsers don't understand PUT/PATCH/DELETE in forms, so a workaround was needed.  Rails looks at a &lt;code&gt;_method&lt;/code&gt; GET parameter on POST requests to determine what HTTP verb it should be recognized as.  The &lt;a href="http://code.google.com/apis/gdata/docs/2.0/basics.html#UpdatingEntry"&gt;GData API&lt;/a&gt; supports this behavior through the &lt;code&gt;X-HTTP-Method-Override&lt;/code&gt; header.&lt;br /&gt;A typical Rails controller might look like this:&lt;br /&gt;&lt;br /&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WidgetsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;&lt;br /&gt;  &lt;span class="c1"&gt;# DELETE /widgets/1&lt;/span&gt;&lt;br /&gt;  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;br /&gt;    &lt;span class="vi"&gt;@widget&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destroy&lt;/span&gt;&lt;br /&gt;    &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="s1"&gt;'/widgets'&lt;/span&gt;&lt;br /&gt;  &lt;span class="k"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;span class="k"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;If you don't like Rails, just close your eyes and think of your favorite web framework...&lt;br /&gt;This action works great for a simple form in a browser.  You click "Submit", it POSTs to the server, and then you end up back at the root page. Then, you can add some jQuery to spice things up for newer browsers. Progressive enhancement and all that.&lt;br /&gt;&lt;br /&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="javascript"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.remove-widget'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;del&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class="c1"&gt;// celebrate, disable a spinner, etc&lt;/span&gt;&lt;br /&gt;  &lt;span class="p"&gt;})&lt;/span&gt;&lt;br /&gt;  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;br /&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="javascript"&gt;&lt;span class="p"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;This works great in all modern browsers, except IE9.  We discovered that not only does IE9 send a real DELETE request, it also &lt;em&gt;follows the redirect&lt;/em&gt; with another DELETE.  If that redirect points to another resource, you can get a dangerous cascading effect.&lt;br /&gt;&lt;a href="http://tools.ietf.org/html/rfc2616#section-10.3.3"&gt;RFC 2616&lt;/a&gt; is not clear about what to do in this case, but strongly suggests that redirects are not automatically followed unless coming from a &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html"&gt;safe method&lt;/a&gt;.&lt;br /&gt;&lt;blockquote&gt;If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.&lt;/blockquote&gt;Standard practice for browsers over the years is that redirects from POST requests are followed with a GET request.  GET/HEAD requests are &lt;a href="http://37signals.com/svn/archives2/google_web_accelerator_hey_not_so_fast_an_alert_for_web_app_designers.php"&gt;usually safe&lt;/a&gt;, so this seems like reasonable behavior.  It's expected by web developers, and consistent across browsers.&lt;br /&gt;&lt;br /&gt;I can't imagine that this behavior in IE9 was on purpose.  It feels like an edge case that slipped through an if statement because &lt;code&gt;"DELETE" != "POST"&lt;/code&gt;.  I've submitted feedback to the IE9 team about this issue.  I'm curious to see what they say.&lt;br /&gt;&lt;br /&gt;So, if your application might be responding to ajax requests with redirects, you should probably start sending back &lt;code&gt;200 OK&lt;/code&gt;...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Update:&lt;/strong&gt; Eric Law on the &lt;a href="http://blogs.msdn.com/b/ieinternals/archive/2011/08/19/understanding-the-impact-of-redirect-response-status-codes-on-http-methods-like-head-get-post-and-delete.aspx"&gt;IEInternals blog&lt;/a&gt; responded to one of Kyle's tweets.  Apparently the behavior is correct according to HTTP 1.0, and IE has been following DELETE redirects since at least IE6.&lt;br /&gt;Here's the breakdown of browser behavior when receiving a 302 redirect from a DELETE request:&lt;br /&gt;&lt;br /&gt;&lt;table border="0" cellpadding="0" cellspacing="0" class="humblebrag"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td valign="top"&gt;IE 6-10&lt;/td&gt; &lt;td valign="top"&gt;DELETE method is preserved&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td valign="top"&gt;Chrome 13&lt;/td&gt; &lt;td valign="top"&gt;Converts to GET&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td valign="top"&gt;Firefox 6&lt;/td&gt; &lt;td valign="top"&gt;Converts to GET&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td valign="top"&gt;Safari 5.1&lt;/td&gt; &lt;td valign="top"&gt;Converts to GET&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td valign="top"&gt;Opera 11.5&lt;/td&gt; &lt;td valign="top"&gt;Converts to GET&lt;br /&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;We didn't see the behavior in IE8, so we assumed it was new to IE9. At least, no one was sending in crazy bug reports from other browsers. This is another example why developers hate dealing with IE.  Kudos to the standards compliance, though!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-1326485023731518221?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/1326485023731518221/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/ie9-deletes-stuff.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/1326485023731518221'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/1326485023731518221'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/ie9-deletes-stuff.html' title='IE9 DELETES stuff'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-2254104502983876483</id><published>2012-02-05T22:05:00.000-08:00</published><updated>2012-02-08T00:50:52.264-08:00</updated><title type='text'>Installing a gem fork from Github source</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;THANKS TO:-&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; http://raflabs.com/blogs/silence-is-foo/2010/07/19/installing-a-gem-fork-from-github-source/&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Usually, installing a gem is as easy as issuing:&lt;br /&gt;&lt;br /&gt;&lt;div class="syntaxhighlighter " id="highlighter_989300"&gt;&lt;div class="lines"&gt;&lt;div class="line alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;1.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;gem &lt;/code&gt;&lt;code class="functions"&gt;install&lt;/code&gt; &lt;code class="plain"&gt;gem_name&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;but, what if you want to install a fork of a gem?&lt;br /&gt;As you may know Github no longer hosts gems, so doing the following won't work:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;$ sudo gem install technoweenie-grit --source http://gems.github.com&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;NOTE: Actually it'd work if the gem you're trying to install is in this list: &lt;a href="http://gems.github.com/list.html" target="_blank"&gt;http://gems.github.com/list.html&lt;/a&gt;&lt;br /&gt;&lt;h3&gt;Disecting the "Download Source" button&lt;/h3&gt;Let's suppose we want to install the &lt;a href="http://techno-weenie.net/" target="_blank"&gt;Techno Weenie&lt;/a&gt;'s fork of &lt;a href="http://grit.rubyforge.org/" target="_blank"&gt;Grit&lt;/a&gt; from &lt;a href="http://www.github.com/"&gt;Github&lt;/a&gt;:&lt;br /&gt;Main repo =&amp;gt; &lt;a href="http://github.com/mojombo/grit" target="_blank"&gt;http://github.com/mojombo/grit&lt;/a&gt;&lt;br /&gt;&lt;a href="http://github.com/defunkt/github-gem" target="_blank"&gt;&lt;/a&gt;Techno Weenie's fork =&amp;gt; &lt;a href="http://github.com/technoweenie/grit" target="_blank"&gt;http://github.com/technoweenie/grit&lt;/a&gt;&lt;br /&gt;Github has a "Download Source" button, if you click it you'll be  presented with two big images (say buttons): "ZIP" and "TAR", clicking  these buttons will start downloading the code of the &lt;strong&gt;master branch&lt;/strong&gt;  in the selected format; below those buttons you'll find the list of  tags of the repository, you'll be able to click one of the tags and  again you'll be presented with "ZIP" and "TAR" to download the source of  such tag.&lt;br /&gt;&lt;h4&gt;How does this works?&lt;/h4&gt;This is how the URL is built:&lt;br /&gt;&lt;br /&gt;http://github.com/USER/REPOSITORY/FORMAT/BRANCH_OR_TAG&lt;br /&gt;&lt;br /&gt;"ZIP" =&amp;gt; http://github.com/technoweenie/grit/&lt;strong&gt;&lt;span style="color: red;"&gt;zipball&lt;/span&gt;&lt;/strong&gt;/master&lt;br /&gt;"TAR" =&amp;gt; http://github.com/technoweenie/grit/&lt;span style="color: red;"&gt;&lt;strong&gt;tarball&lt;/strong&gt;&lt;/span&gt;/master&lt;br /&gt;said this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="syntaxhighlighter " id="highlighter_551458"&gt;&lt;div class="lines"&gt;&lt;div class="line alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;1.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;$ wget http://github.com/technoweenie/grit/zipball/master&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;2.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;3.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;$ unzip technoweenie-grit-v2.0.0-12-g0bd0c5f.zip&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;4.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;5.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;$ &lt;/code&gt;&lt;code class="functions"&gt;cd&lt;/code&gt; &lt;code class="plain"&gt;technoweenie-grit-0bd0c5f&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h4&gt;&lt;br /&gt;&lt;/h4&gt;&lt;h4&gt;Installing the gem&lt;/h4&gt;Gems source code has a special file called gem_name.gemspec, this is like the DNA of the gem and it's used to build it:&lt;br /&gt;&lt;br /&gt;&lt;div class="syntaxhighlighter " id="highlighter_888072"&gt;&lt;div class="lines"&gt;&lt;div class="line alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;1.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;$ gem build grit.gemspec&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;Issuing this command will generate a gem_name-version.gem file which is the gem itself, let's install it:&lt;br /&gt;&lt;br /&gt;&lt;div class="syntaxhighlighter " id="highlighter_681496"&gt;&lt;div class="lines"&gt;&lt;div class="line alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;1.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;$ &lt;/code&gt;&lt;code class="functions"&gt;sudo&lt;/code&gt; &lt;code class="plain"&gt;gem &lt;/code&gt;&lt;code class="functions"&gt;install&lt;/code&gt; &lt;code class="plain"&gt;grit-2.0.0.gem&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;Some projects doesn't have a .gemspec file but rake tasks, in that case you just need to do:&lt;br /&gt;&lt;br /&gt;&lt;div class="syntaxhighlighter " id="highlighter_260146"&gt;&lt;div class="lines"&gt;&lt;div class="line alt1"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;1.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;$ rake build&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="line alt2"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;code class="number"&gt;2.&lt;/code&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="plain"&gt;$ rake &lt;/code&gt;&lt;code class="functions"&gt;install&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px !important;"&gt;&lt;code class="functions"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;If any of these methods work, you'd have to look at the Rakefile for tasks to generate the .gemspec file.&lt;br /&gt;...and we're done!&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color: red;"&gt;NOTE&lt;/span&gt;&lt;/strong&gt;: 'gem  build' will not take in consideration special and external dependencies  the gem might require, so you must take care of the dependencies  manually. Check in the code all the 'require' lines to see dependencies.&lt;br /&gt;Hope it helps.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-2254104502983876483?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/2254104502983876483/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/installing-gem-fork-from-github-source.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/2254104502983876483'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/2254104502983876483'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/installing-gem-fork-from-github-source.html' title='Installing a gem fork from Github source'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-7965220184157931674</id><published>2012-02-05T21:37:00.000-08:00</published><updated>2012-02-08T00:53:52.002-08:00</updated><title type='text'>Gems On Rails Get Version 0.7.2 → ‘gemsonrails’</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;THANKS TO:-&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #660000;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;http://gemsonrails.rubyforge.org and&amp;nbsp;&lt;a href="mailto:drnicwilliams@gmail.com"&gt;Dr Nic Williams&lt;/a&gt;&amp;nbsp;from where i got this information&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;span class="Apple-style-span" style="color: #cc0000;"&gt;BASIC WORK&lt;/span&gt;&lt;/h2&gt;Link or freeze RubyGems into your rails apps, instead of just  plugins. This allows you to ‘vendor everything’ – pushing all dependent  gems into your rails app thus ensuring your application will be  guaranteed to work when deployed. Your application is no longer  dependent on the gems that are/aren’t available on your target  deployment environment.&lt;br /&gt;&lt;h2&gt;&lt;span class="Apple-style-span" style="color: #cc0000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;h2&gt;&lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Installation and The Basics&lt;/span&gt;&lt;/h2&gt;First you install the gem:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;$ sudo gem install gemsonrails&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;Then you deploy the gem into your target Rails application:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;$ cd rails_app_folder&lt;br /&gt;$ gemsonrails&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;This installs some rake tasks into your app to help you freeze or link gems:&lt;br /&gt;&lt;br /&gt;&lt;pre syntax="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;$ rake -T | grep "gems:" &lt;br /&gt;rake gems:freeze    # Freeze a RubyGem into this Rails application; init.rb will be loaded on startup.&lt;br /&gt;rake gems:link      # Link a RubyGem into this Rails application; init.rb will be loaded on startup.&lt;br /&gt;rake gems:unfreeze  # Unfreeze/unlink a RubyGem from this Rails application&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre syntax="ruby"&gt;&lt;/pre&gt;&lt;pre syntax="ruby"&gt;&lt;/pre&gt;&lt;pre syntax="ruby"&gt;&lt;/pre&gt;Finally, for each gem your application uses you can either freeze a  version into the app (only for gems that do not require native  compilation), or link a gem with your app (useful for gems that require  native compilation)&lt;br /&gt;&lt;br /&gt;&lt;pre syntax="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;$ rake gems:freeze GEM=map_by_method&lt;br /&gt;Unpacked map_by_method 0.6.0 to 'map_by_method-0.6.0'&lt;br /&gt;&lt;br /&gt;$ ls vendor/gems/map_by_method-0.6.0/&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre syntax="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;CHANGELOG    README    Rakefile     init.rb      lib     test&lt;br /&gt;&lt;br /&gt;$ cat vendor/gems/map_by_method-0.6.0/init.rb &lt;br /&gt;require File.join(File.dirname(__FILE__), 'lib', 'map_by_method')&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;Restricting which environments a gem is loaded into&lt;/h2&gt;You can restrict which &lt;span class="caps"&gt;RAILS&lt;/span&gt;_ENVs the gem will be loaded for, using &lt;span class="caps"&gt;ONLY&lt;/span&gt;; e.g.&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre syntax="ruby"&gt;&lt;span class="Apple-style-span" style="color: #351c75; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;b&gt;$ rake gems:freeze GEM=&amp;lt;gemname&amp;gt; ONLY=development&lt;br /&gt;$ rake gems:link GEM=&amp;lt;gemname&amp;gt; ONLY=production,staging&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre syntax="ruby"&gt;&lt;/pre&gt;&lt;pre syntax="ruby"&gt;&lt;/pre&gt;In the former, only in &lt;strong&gt;development&lt;/strong&gt; mode will the gem be loaded. In the latter, only in &lt;strong&gt;production&lt;/strong&gt; or &lt;strong&gt;staging&lt;/strong&gt; mode will the gem be loaded from the deployment gem server&lt;br /&gt;&lt;h2&gt;Upgrading from old GemsOnRails versions&lt;/h2&gt;All the freezing and linking functionality is in the plugin installed by the &lt;code&gt;gemsonrails&lt;/code&gt; command. To upgrade, simple install the new gem version, and re-run the &lt;code&gt;gemsonrails&lt;/code&gt; command. You application will now have the newest rake tasks and functionality.&lt;br /&gt;&lt;h2&gt;&lt;br /&gt;&lt;/h2&gt;&lt;h2&gt;Forum&lt;/h2&gt;&lt;a href="http://groups.google.com/group/gemsonrails"&gt;http://groups.google.com/group/gemsonrails&lt;/a&gt;&lt;br /&gt;&lt;h2&gt;How to submit patches&lt;/h2&gt;Read the &lt;a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/"&gt;8 steps for fixing other people’s code&lt;/a&gt; and for section &lt;a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups"&gt;8b: Submit patch to Google Groups&lt;/a&gt;, use the Google Group above.&lt;br /&gt;&lt;h2&gt;Licence&lt;/h2&gt;This code is free to use under the terms of the &lt;span class="caps"&gt;MIT&lt;/span&gt; licence.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-7965220184157931674?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/7965220184157931674/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/02/gems-on-rails-get-version-072.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/7965220184157931674'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/7965220184157931674'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/02/gems-on-rails-get-version-072.html' title='Gems On Rails Get Version 0.7.2 → ‘gemsonrails’'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-3688265118471253498</id><published>2012-01-15T22:06:00.000-08:00</published><updated>2012-01-15T22:09:37.365-08:00</updated><title type='text'>Bort: Better Skeleton Applications</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;We’re all familiar with the &lt;code&gt;rails&lt;/code&gt; command-line tool. It  generates a Rails skeleton application with everything you need to start  your Rails application–directories, configuration, even a default index  page.&lt;br /&gt;But the more you develop Rails applications, the more you begin to realize that some tools and gems are valuable on &lt;em&gt;every&lt;/em&gt; project. Need a user management system? Likely &lt;code&gt;restful_authentication&lt;/code&gt;. Rails pagination slow? Grab &lt;code&gt;will_paginate&lt;/code&gt;. Even things like setting sessions to use the database, or using MySQL instead of sqlite3 as your database.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Enter bort. 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 &lt;em&gt;is&lt;/em&gt; a bigger box–it includes:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;RESTful Authentication&lt;/strong&gt;, for a RESTful user-management system (registering users, logging in, etc.). Even includes the forgotten password feature!&lt;/li&gt;&lt;li&gt;&lt;strong&gt;User Roles&lt;/strong&gt;, to make your application users easier to manage; includes a default admin role and an admin user.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Will Paginate&lt;/strong&gt;, an efficient, pretty pagination plugin. Beats the standard Rails pagination by ages!&lt;/li&gt;&lt;li&gt;&lt;strong&gt;OpenID integration&lt;/strong&gt;, in case you want to support OpenID with your users.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Rspec&lt;/strong&gt;, a better way to write tests.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Exception Notifier&lt;/strong&gt;, to email you when your application hiccups.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Asset Packager&lt;/strong&gt;, which combines javascript and CSS to save download time.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Database Sessions, MySQL DB YAML, an application-wide layout file, and no default index.html and rails.png&lt;/strong&gt;, all of which make your life a little easier.&lt;/li&gt;&lt;/ul&gt;This is a big step up from your standard Rails skeleton. Best of all,  if you don’t use half of this stuff, it won’t affect you much; and  it’ll be there when you need it.&lt;br /&gt;&lt;br /&gt;You can&amp;nbsp;Grab bort from the &lt;a href="http://github.com/fudgestudios/bort/tree/master"&gt;GitHub page&lt;/a&gt;,&lt;br /&gt;&lt;a href="http://github.com/fudgestudios/bort"&gt;http://github.com/fudgestudios/bort&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-3688265118471253498?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/3688265118471253498/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/01/bort-better-skeleton-applications.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/3688265118471253498'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/3688265118471253498'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/01/bort-better-skeleton-applications.html' title='Bort: Better Skeleton Applications'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-4513414862342842771</id><published>2012-01-13T00:14:00.000-08:00</published><updated>2012-01-13T00:15:41.889-08:00</updated><title type='text'>Rails "The MVC Architecture"</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;At the core of Rails is the Model, View, Controller architecture, usually just called &lt;span class="caps"&gt;MVC&lt;/span&gt;. &lt;span class="caps"&gt;MVC&lt;/span&gt; benefits include:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Isolation of business logic from the user interface&lt;/li&gt;&lt;li&gt;Ease of keeping code &lt;span class="caps"&gt;DRY&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Making it clear where different types of code belong for easier maintenance&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;h5 id="models"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&amp;nbsp;Models&lt;/span&gt;&lt;/h5&gt;&lt;br /&gt;The &lt;b&gt;model&lt;/b&gt; manages the behavior and data of the application  domain, responds to requests for information about its state (usually  from the view), and responds to instructions to change state (usually  from the controller). In event-driven systems, the model notifies  observers (usually views) when the information changes so that they can  react.&lt;br /&gt;&lt;br /&gt;A &lt;b&gt;model&lt;/b&gt; represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, each table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.&lt;br /&gt;&lt;h5 id="views"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h5&gt;&lt;h5 id="views"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&amp;nbsp;Views&lt;/span&gt;&lt;/h5&gt;&lt;br /&gt;The &lt;b&gt;view&lt;/b&gt; renders the model into a form suitable for interaction,  typically a user interface element. Multiple views can exist for a  single model for different purposes. A view port typically has a one to  one correspondence with a display surface and knows how to render to it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Views&lt;/b&gt; represent the user interface of your application. In Rails, views are often &lt;span class="caps"&gt;HTML&lt;/span&gt; files with embedded Ruby code that perform tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.&lt;br /&gt;&lt;br /&gt;&lt;h5 id="controllers"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Controllers&lt;/span&gt;&lt;/h5&gt;&lt;br /&gt;The &lt;b&gt;controller&lt;/b&gt; receives user input and initiates a response by  making calls on model objects. A controller accepts input from the user  and instructs the model and a view port to perform actions based on that  input.&lt;br /&gt;&lt;br /&gt;Controllers provide the “glue” between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-4513414862342842771?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/4513414862342842771/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2012/01/rails-mvc-architecture.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/4513414862342842771'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/4513414862342842771'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2012/01/rails-mvc-architecture.html' title='Rails &quot;The MVC Architecture&quot;'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7474356913652317146.post-6491736611447332216</id><published>2011-12-14T22:22:00.000-08:00</published><updated>2011-12-14T22:22:48.217-08:00</updated><title type='text'>Ruby  Basic "Variables"</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;h1 style="font-weight: normal;"&gt;&lt;span style="font-size: small;"&gt;R&lt;span style="font-size: large;"&gt;uby is a loosely typed language means if we want to declare any variable of different datatype we don't need to write like other language like in Java here we can directly assign value to a variable and it belong to that datatype class which kind of value its have example :-&lt;/span&gt;&lt;/span&gt;&lt;/h1&gt;&lt;pre class="code-java"&gt;&lt;span style="color: #990000; font-size: large;"&gt;a = 5        # &lt;span class="code-quote"&gt;"a"&lt;/span&gt; type as Fixnum&lt;br /&gt;&lt;br /&gt;a = &lt;span class="code-quote"&gt;"text"&lt;/span&gt;   # &lt;span class="code-quote"&gt;"a"&lt;/span&gt; is re-typed as &lt;span class="code-object"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;h1 style="font-weight: normal;"&gt;&lt;/h1&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7474356913652317146-6491736611447332216?l=rubyguide.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rubyguide.blogspot.com/feeds/6491736611447332216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rubyguide.blogspot.com/2011/12/ruby-basic-variables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/6491736611447332216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7474356913652317146/posts/default/6491736611447332216'/><link rel='alternate' type='text/html' href='http://rubyguide.blogspot.com/2011/12/ruby-basic-variables.html' title='Ruby  Basic &quot;Variables&quot;'/><author><name>Learner</name><uri>http://www.blogger.com/profile/15543935902457127080</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
