<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>PaulBarry.com - View Source for Ruby Code</title>
  <subtitle type="html">My thoughts, ideas, questions and concerns on technology, sports, music and life</subtitle>
  <id>tag:paulbarry.com,2007:Paulbarry.com</id>
  <generator uri="http://www.paulbarry.com" version="3.0">PaulBarry.com</generator>
  <link href="http://paulbarry.com/xml/atom/article/4899/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://paulbarry.com/articles/2008/05/06/view-source-for-ruby-code" rel="alternate" type="text/html"/>

  <updated>2008-09-08T04:36:09-04:00</updated>
  <entry>
    <author>
      <name>Paul Barry</name>
      <email>mail@paulbarry.com</email>
    </author>
    <id>urn:uuid:1fcf6526-df5c-424c-95b4-0fcbc85ff9ff</id>

    <published>2008-05-06T10:15:42-04:00</published>
    <updated>2008-05-06T10:15:42-04:00</updated>
    <title type="html">View Source for Ruby Code</title>
    <link href="http://paulbarry.com/articles/2008/05/06/view-source-for-ruby-code" rel="alternate" type="text/html"/>

    <category term="general" scheme="http://paulbarry.com/articles/category/general" label="General"/>
        <category term="Ruby2Ruby" scheme="http://paulbarry.com/articles/tag/ruby2ruby"/>
    <category term="Rails" scheme="http://paulbarry.com/articles/tag/rails"/>
    <category term="Ruby" scheme="http://paulbarry.com/articles/tag/ruby"/>
        <summary type="html">&lt;p&gt;One thing that all web developers do at some is view the HTML source of the page your application has generated.  The reason why is because what your web application does is dynamically generate HTML and you want to see what HTML it generated.  You can think of Ruby, as a metaprogramming language, as a language that dynamically generates code.  If you are doing Rails, you see this all of the time with association methods like &lt;code&gt;has_and_belongs_to_many&lt;/code&gt;.  What &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; does is generate methods on your class.  For example, if you have an Product class that &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; categories, it will add a &lt;code&gt;category_ids=&lt;/code&gt; method to your class.  This all happens at runtime, so you can&apos;t see the method, but if you use Ruby2Ruby, you can.  &lt;code&gt;sudo gem install ruby2ruby&lt;/code&gt; and then run &lt;code&gt;script/console&lt;/code&gt; at the root of your Rails app.  Then just run these simple commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; require &apos;ruby2ruby&apos;
&amp;gt;&amp;gt; puts Ruby2Ruby.translate(Article)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The output will be the ruby code that your class now has.  Somewhere in that code you will see:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def category_ids=(new_value)
  ids = (new_value or []).reject { |nid| nid.blank? }
  send(&quot;#{reflection.name}=&quot;, reflection.class_name.constantize.find(ids))
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which is the &lt;code&gt;category_ids=&lt;/code&gt; method that was generated by &lt;code&gt;has_and_belongs_to_many&lt;/code&gt;.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;One thing that all web developers do at some is view the HTML source of the page your application has generated.  The reason why is because what your web application does is dynamically generate HTML and you want to see what HTML it generated.  You can think of Ruby, as a metaprogramming language, as a language that dynamically generates code.  If you are doing Rails, you see this all of the time with association methods like &lt;code&gt;has_and_belongs_to_many&lt;/code&gt;.  What &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; does is generate methods on your class.  For example, if you have an Product class that &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; categories, it will add a &lt;code&gt;category_ids=&lt;/code&gt; method to your class.  This all happens at runtime, so you can&apos;t see the method, but if you use Ruby2Ruby, you can.  &lt;code&gt;sudo gem install ruby2ruby&lt;/code&gt; and then run &lt;code&gt;script/console&lt;/code&gt; at the root of your Rails app.  Then just run these simple commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; require &apos;ruby2ruby&apos;
&amp;gt;&amp;gt; puts Ruby2Ruby.translate(Article)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The output will be the ruby code that your class now has.  Somewhere in that code you will see:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def category_ids=(new_value)
  ids = (new_value or []).reject { |nid| nid.blank? }
  send(&quot;#{reflection.name}=&quot;, reflection.class_name.constantize.find(ids))
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which is the &lt;code&gt;category_ids=&lt;/code&gt; method that was generated by &lt;code&gt;has_and_belongs_to_many&lt;/code&gt;.&lt;/p&gt;
</content>
  </entry>
  </feed>