<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Sanjeev Mishra's Blog</title>
	<atom:link href="http://sanjusoftware.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sanjusoftware.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 15 Dec 2011 05:19:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sanjusoftware.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Sanjeev Mishra's Blog</title>
		<link>http://sanjusoftware.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sanjusoftware.wordpress.com/osd.xml" title="Sanjeev Mishra&#039;s Blog" />
	<atom:link rel='hub' href='http://sanjusoftware.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Dynamically added fields using ajax do not get posted on form submission (FORM inside TABLE)</title>
		<link>http://sanjusoftware.wordpress.com/2011/01/11/dynamically-added-fields-using-ajax-do-not-get-posted-on-form-submission-form-inside-table/</link>
		<comments>http://sanjusoftware.wordpress.com/2011/01/11/dynamically-added-fields-using-ajax-do-not-get-posted-on-form-submission-form-inside-table/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 06:26:58 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/2011/01/11/dynamically-added-fields-using-ajax-do-not-get-posted-on-form-submission-form-inside-table/</guid>
		<description><![CDATA[This is a problem which is actually not a problem if we are following right semantics of HTML. Problem: I faced this problem where in after a page is loaded I have to add certain input html elements in the form (which I had inside a table) using ajax. Only the elements that were loaded [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=66&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a problem which is actually not a problem if we are following right semantics of HTML.</p>
<p>Problem: I faced this problem where in after a page is loaded I have to add certain input html elements in the form (which I had inside a table) using ajax. Only the elements that were loaded when the page rendered were getting posted, but not the ones added using ajax after the page was loaded. This can be frustrating to figure out why the elements which were loaded by default are getting posted but the elements which rendered after wards using AJAX are not. </p>
<p>My Theory: When a DOM is loaded, the input elements, which are rendered inside TABLE cells get hooked to the FORM automatically because they are rendered when the DOM got loaded. And when the form is posted, its not the table, but the input elements which are hooked to the form get posted. Now, if you are rendering some elements through ajax, then those elements get hooked up only to the TABLE (in case the FORM is inside the TABLE) and not to the form.</p>
<p>Solution: Shift the TABLE inside the FORM. So, that when you add more elements dynamically, it gets hooked to the form as well.</p>
<p>FORM<br />
     TABLE &#8212; WORKS</p>
<p>TABLE<br />
    FORM &#8212; NOT GOOD</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=66&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2011/01/11/dynamically-added-fields-using-ajax-do-not-get-posted-on-form-submission-form-inside-table/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails Fixtures load order</title>
		<link>http://sanjusoftware.wordpress.com/2010/12/17/rails-fixtures-load-order/</link>
		<comments>http://sanjusoftware.wordpress.com/2010/12/17/rails-fixtures-load-order/#comments</comments>
		<pubDate>Fri, 17 Dec 2010 06:27:22 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[ActiveFixture]]></category>
		<category><![CDATA[fixtires]]></category>
		<category><![CDATA[fixtures load error]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/?p=50</guid>
		<description><![CDATA[This was a head breaking experience for me recently. We all know that the fixtures files in rails load by default in alphabetical order. And the records inside every single file again load in alphabetical order (not from top to bottom as you define in the fixture file). Considering that having database level constraints (foreign [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=50&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This was a head breaking experience for me recently.</p>
<p>We all know that the fixtures files in rails load by default in alphabetical order. And the records inside every single file again load in alphabetical order (not from top to bottom as you define in the fixture file). Considering that having database level constraints (foreign keys, primary keys etc) is advisable idea in spite of having the rails model level relations (belongs_to, has_many etc), loading fixtures in alphabetical order is the most stupid thing I can ever think of. Imagine you have Account and Employee models and employee has_many accounts. So you have an employee_id sitting as a foreign key in the accounts table. Now you want to load your fixtures (rake db:fixtures:load), for test purpose say, which was my requirement. Since &#8216;A&#8217; comes before &#8216;E&#8217; in alphabetical order, rails tries to load the addresses first and then the employees fixture file. Obviously this fails because of foreign key violations. Database raising errors is perfectly making sense here. So I looked around for something  which may solve this problem and found that there are solutions available given by some people. But what surprised me is that there is nothing that rails core provided for this.</p>
<p>Anyways, so for people who are into this kinda problem, here is the best one called <a title="ActiveFixture" href="http://alexle.net/archives/217" target="_blank">ActiveFixture</a> that I came across and wish that it would get included in the rails core someday.</p>
<p>Again there is something that may surprise you again. Its not that rails has been stupid all these days to leave us in such a situation. It might so happen that you may not do anything for ordering of the fixtures and still would not get any error although you have foreign key relationships in the database. The reason actually lies in the active record&#8217;s database (postgres, mysql sql-lite etc) adapters class. Here is how it goes. While loading the fixtures (I am giving postgres example, as I am using postgres for this project and found it there), rails calls the following method in ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:</p>
<p><code>def supports_disable_referential_integrity?() #:nodoc:<br />
  version = query("SHOW server_version")[0][0].split('.')<br />
  (version[0].to_i &gt;= 8 &amp;&amp; version[1].to_i &gt;= 1) ? true : false<br />
rescue<br />
   return false<br />
end</code></p>
<p>So this method says, that if you pgsql version is x.y.z, then if  x &gt;= 8 and y &gt;= 1, then it will be able to defer the referential integrity for you and you could load the data from fixtures into your database easily without taking care of the ordering and without facing any data integrity errors <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  Unfortunately I, in my team, was using 9.y.z version of pgsql and all my collegues were on 8.1.4. So they were not getting any errors and I was frustated to know WHY.</p>
<p>Well the secret lies in the ConnectionAdapters for rails <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  I don&#8217;t know if we should take it as an advantage, that rails is trying to do or not, but until we have something more intelligent to infer the order of loading the fixtures files from the relationships in the models, rather than defining the load order manually as mentioned <a href="http://codesnippets.joyent.com/posts/show/320" target="_blank">here</a>, I am happy that its saving me some time.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=50&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2010/12/17/rails-fixtures-load-order/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>`bin_path’: can’t find executable rake for rake-0.8.7 (Gem::Exception)</title>
		<link>http://sanjusoftware.wordpress.com/2010/12/04/bin_path%e2%80%99-can%e2%80%99t-find-executable-rake-for-rake-0-8-7-gemexception/</link>
		<comments>http://sanjusoftware.wordpress.com/2010/12/04/bin_path%e2%80%99-can%e2%80%99t-find-executable-rake-for-rake-0-8-7-gemexception/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 11:56:49 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/2010/12/04/bin_path%e2%80%99-can%e2%80%99t-find-executable-rake-for-rake-0-8-7-gemexception/</guid>
		<description><![CDATA[Well after a long time, I finally had to shift back to ROR development on Windows. I am running on Windows Vista, Rails 3, ruby 1.9.2. Things are not all that bad on windows, or at least, not as much as I thought they might be. But, we definitely keep on facing problems here and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=48&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Well after a long time, I finally had to shift back to ROR development on Windows. I am running on Windows Vista, Rails 3, ruby 1.9.2. Things are not all that bad on windows, or at least, not as much as I thought they might be. But, we definitely keep on facing problems here and there. There is one such which you also might face if you try development on Rails 3 with ruby 1.9.2. There seems to be a bug with the gems that are installed with the one click windows ruby installer. I am not sure why, but you may lead to see the following error message once you start using rake:</p>
<p><code>`bin_path’: can’t find executable rake for rake-0.8.7 (Gem::Exception)</code></p>
<p>Now the solution to get away from this bug as of this writing is that you will have to remove the rake.gemspec file from the &#8220;your_path_to\Ruby192\lib\ruby\gems\1.9.1\specifications&#8221; folder.</p>
<p>And you are good to go now. I do not have really too much time to dig into the matter as to why this might be happening, but it worked for me.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=48&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2010/12/04/bin_path%e2%80%99-can%e2%80%99t-find-executable-rake-for-rake-0-8-7-gemexception/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>Configuring delayed_job 2.x</title>
		<link>http://sanjusoftware.wordpress.com/2010/10/06/configuring-delayed_job-2-x/</link>
		<comments>http://sanjusoftware.wordpress.com/2010/10/06/configuring-delayed_job-2-x/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 13:50:28 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/2010/10/06/configuring-delayed_job-2-x/</guid>
		<description><![CDATA[I was configuring the delayed job and I got problems like : `method_missing_without_paginate&#8217;: undefined method `destroy_failed_jobs=&#8217; This is because I have 2.0.3 version of delayed job&#8230; The configuration parameters have all been moved to Delayed::Worker now and thus the following works for version 2.x: Delayed::Worker.destroy_failed_jobs = false Delayed::Worker.sleep_delay = 60 Delayed::Worker.max_attempts = 3 Delayed::Worker.max_run_time = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=44&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was configuring the delayed job and I got problems like :</p>
<p>`method_missing_without_paginate&#8217;: undefined method `destroy_failed_jobs=&#8217;</p>
<p>This is because I have 2.0.3 version of delayed job&#8230; The configuration parameters have all been moved to Delayed::Worker now and thus the following works for version 2.x:</p>
<p><code>Delayed::Worker.destroy_failed_jobs = false<br />
Delayed::Worker.sleep_delay = 60<br />
Delayed::Worker.max_attempts = 3<br />
Delayed::Worker.max_run_time = 5.minutes</code></p>
<p>or the following:</p>
<p><code>Delayed::Worker.destroy_failed_jobs = false<br />
silence_warnings do<br />
  Delayed::Worker.const_set("MAX_ATTEMPTS", 3)<br />
  Delayed::Worker.const_set("MAX_RUN_TIME", 5.minutes)<br />
end</code></p>
<p>instead of </p>
<p><code>Delayed::Job.destroy_failed_jobs = false<br />
silence_warnings do<br />
  Delayed::Job.const_set("MAX_ATTEMPTS", 3)<br />
  Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes)<br />
end</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=44&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2010/10/06/configuring-delayed_job-2-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>Awesome date time plugin</title>
		<link>http://sanjusoftware.wordpress.com/2010/09/24/awesome-date-time-plugin/</link>
		<comments>http://sanjusoftware.wordpress.com/2010/09/24/awesome-date-time-plugin/#comments</comments>
		<pubDate>Fri, 24 Sep 2010 13:43:04 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/2010/09/24/awesome-date-time-plugin/</guid>
		<description><![CDATA[After a long time, I have seen such a great datetime picker for rails. &#8220;Awesome&#8221; is just not the word for it<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=38&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After a long time, I have seen such <a title="datetime picker" href="http://2007.electronicholas.com/calendar" target="_blank">a great datetime picker</a> for rails. &#8220;Awesome&#8221; is just not the word for it <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=38&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2010/09/24/awesome-date-time-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby Class with Mass assignable attributes</title>
		<link>http://sanjusoftware.wordpress.com/2010/09/23/ruby-class-with-mass-assignable-attributes/</link>
		<comments>http://sanjusoftware.wordpress.com/2010/09/23/ruby-class-with-mass-assignable-attributes/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 16:55:39 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/2010/09/23/ruby-class-with-mass-assignable-attributes/</guid>
		<description><![CDATA[Well, this is very weired again, but it seems ruby class does not allow me to have mass assignable attributes as you could do with the ActiveRecord models using attr_accessible or attr_accessor. Its very painful to have too many getters and setters in the class and to assign them manually when you create an object [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=32&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Well, this is very weired again, but it seems ruby class does not allow me to have mass assignable attributes as you could do with the ActiveRecord models using attr_accessible or attr_accessor.</p>
<p>Its very painful to have too many getters and setters in the class and to assign them manually when you create an object of that class or to have constructor which has those hundreds of params.</p>
<p>So, how do we achieve that. Well there are two ways.</p>
<p><strong>1. The hacky way:</strong><br />
<code><br />
class Woo<br />
  attr_accessor :f_name, :l_name<br />
  def initialize(params)<br />
   #instance_eval &amp;block<br />
   params.each do |key, value|<br />
      eval("self.#{key}= '#{value}'") if self.respond_to? "#{key}="<br />
   end<br />
  end<br />
end<br />
woo = Woo.new(:f_name =&gt; "first_name", :l_name =&gt; "last_name")<br />
</code><br />
What we are doing here is that we are trying to assign these instance variables in an eval loop. So the constructor to the method just takes all your parameter hash and then tries to see if your class responds to that method setter (which it should as you have defined the att_accessors for those instance variable already), and then assigns them the corresponding value from the hash.</p>
<p><strong>2. </strong><strong>The cleaner way, but which hides what you have inside as attributes</strong><br />
<code><br />
require 'ostruct'<br />
Class woo  &lt; OpenStruct<br />
end<br />
woo = Woo.new(:f_name =&gt; "first_name", :l_name =&gt; "last_name", any_no_of_craps...)<br />
</code></p>
<p>OpenStruct is nothing but another container like a hash in ruby which just gets better when it comes to serialization of these objects (which in my own case is a need)</p>
<p>Hope that helps a bit to someone!!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=32&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2010/09/23/ruby-class-with-mass-assignable-attributes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>no such file to load &#8212; capistrano-ext</title>
		<link>http://sanjusoftware.wordpress.com/2010/09/22/no-such-file-to-load-capistrano-ext/</link>
		<comments>http://sanjusoftware.wordpress.com/2010/09/22/no-such-file-to-load-capistrano-ext/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 14:39:36 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/2010/09/22/no-such-file-to-load-capistrano-ext/</guid>
		<description><![CDATA[If you are using capistrano and capistrano-ext together, please note that the following line in you environment.rb might not be sufficient: config.gem &#8220;capistrano&#8221; config.gem &#8216;capistrano-ext&#8217; In this case, you might get the error : no such file to load &#8212; capistrano-ext bla-bla-bla All you need for your app to find the gem (I am assuming [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=27&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are using capistrano and capistrano-ext together, please note that the following line in you environment.rb might not be sufficient:</p>
<p>config.gem &#8220;capistrano&#8221;<br />
config.gem &#8216;capistrano-ext&#8217;</p>
<p>In this case, you might get the error :</p>
<p>no such file to load &#8212; capistrano-ext<br />
bla-bla-bla</p>
<p>All you need for your app to find the gem (I am assuming that you have already installed the gem by running the command : rake gem install capistrano-ext) is following:</p>
<p>config.gem &#8220;capistrano&#8221;<br />
config.gem &#8216;capistrano-ext&#8217;, <strong>:lib =&gt; &#8216;capistrano&#8217;</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=27&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2010/09/22/no-such-file-to-load-capistrano-ext/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>Mocha should be loaded after Shoulda with rails 2.3</title>
		<link>http://sanjusoftware.wordpress.com/2010/09/22/mocha-should-be-loaded-after-shoulda-with-rails-2-3/</link>
		<comments>http://sanjusoftware.wordpress.com/2010/09/22/mocha-should-be-loaded-after-shoulda-with-rails-2-3/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 09:07:05 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Mocha]]></category>
		<category><![CDATA[Rails 2.3]]></category>
		<category><![CDATA[Shoulda]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/?p=24</guid>
		<description><![CDATA[Well, this one really drove me crazy. So I decided to finally pen it down. May this help somebody else in future. This might not be very new, but really crazy stuff. If you are using Mocha and Shoulda both, then please load Mocha after you have loaded Shoulda. Like following: config.gem &#8220;shoulda&#8221;, :lib =&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=24&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Well, this one really drove me crazy. So I decided to finally pen it down. May this help somebody else in future. This might not be very new, but really crazy stuff.</p>
<p>If you are using Mocha and Shoulda both, then please load Mocha after you have loaded Shoulda. Like following:</p>
<p>config.gem &#8220;shoulda&#8221;, :lib =&gt; &#8220;shoulda&#8221;, :source =&gt; &#8220;http://gems.github.com&#8221;, :version =&gt; &#8220;&gt;=2.11.3&#8243;<br />
config.gem &#8216;mocha&#8217;</p>
<p>If you load Mocha before Shoulda, what happens is that Mocha gets crazy with the expectations. You might see your tests failing because of some weird expectations that are not set in the current failing test but were set somewhere else in some other test.</p>
<p>So, in a nut shell, Mocha starts keeping the expectations across tests instead of wiping them off after each test, as it is expected to do.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=24&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2010/09/22/mocha-should-be-loaded-after-shoulda-with-rails-2-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>class_eval and instance_eval</title>
		<link>http://sanjusoftware.wordpress.com/2009/06/01/class_eval-and-instance_eval/</link>
		<comments>http://sanjusoftware.wordpress.com/2009/06/01/class_eval-and-instance_eval/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 16:44:56 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Metaprograming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/?p=20</guid>
		<description><![CDATA[Hey guys this is the most amazing and funny thing I have learnt in my life time. I started laughing like anything when I learnt how this behaves and things started going over my head for some time Then I happened to watch Dave Thomas&#8217;s video on metaprogramming as many as thrice to really sink [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=20&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hey guys this is the most amazing and funny thing I have learnt in my life time. I started laughing like anything when I learnt how this behaves and things started going over my head for some time <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Then I happened to watch <strong>Dave Thomas&#8217;s video on metaprogramming</strong> as many as thrice to really sink in whats going on. It&#8217;s so dam! confusing in start, I must say. But once you sink in the fact is that even <strong>all the classes in ruby are instances of the type Class</strong> at the end of the day, it all starts making sense.</p>
<p>So lets see what is this:</p>
<p>To start with lets say we have a class <strong>Animal {}</strong></p>
<p>Now if I say, <strong>Animal.class_eval { # some code here may be a method definition}</strong>, what this will do is induce methods on the instance&#8217;s of the class Animal. Yes you read right, it&#8217;s the instances of the class Animal who will get methods defined inside the class_eval block of a class and not the class Animal itself.</p>
<p>Why so&#8230; if at all like this, then <strong>why is this stupid naming convention there to confuse the learners anyways</strong>. Exactly this went just now through your mind, right? So it did through my mind when I read it for the first time. But think it like this. Who is the receiver of the class_eval here. <strong>Receiver is the class Animal</strong>. So the methods will go in Animal class. Now if the methods go in the class Animal, then obviously they are available to the instances of Animal. RIGHT? <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now look at the other one, <strong>Animal.instace_eval {# some code goes here may be some method definition}</strong>. Now where are these methods goign to go. Lets analyze this in a logical manner. Who is the receiver here. <strong>Receiver is instance Animal</strong>. So the methods should go to the instance Animal and not the class Animal. Now, as I said a little while ago, every class is an instance of class Class. And therefore, there methods will go in the metaclass Animal (Represented as A&#8217;). This, in terms of Dave Thomas, is called ghost class or anonymous class which we cannot instantiate but does come in to play when any class is evaluated as an instance.</p>
<p>The above things would be easily get cleared to those who are familiar with the <strong>go-one-step-right</strong> <strong>for methods in ruby funda!</strong></p>
<p>So, for those who do not know this, I would recommend watch the Dae Thomas&#8217;s videos on metaprograming. For those who hae forgotten and need some hint, read the following:</p>
<p>Ruby simply says, that the instances are behaviors are kept side by side, the object (receiver of the method call) being on the left and the methods that can be called on it are on the right side of it.</p>
<p>a -&gt;(instance methods go in one step right to) Animal -&gt; (class methods go in one step right to) Animal&#8217;</p>
<p>Still confusing, then u have really forgotten, go and read more <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for reading (I am really lazy in giving good graphical examples, I am sorry for that. But that you might find out in some good book <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/20/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/20/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/20/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=20&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2009/06/01/class_eval-and-instance_eval/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby Metaprograming</title>
		<link>http://sanjusoftware.wordpress.com/2009/05/25/ruby-metaprograming/</link>
		<comments>http://sanjusoftware.wordpress.com/2009/05/25/ruby-metaprograming/#comments</comments>
		<pubDate>Mon, 25 May 2009 11:55:36 +0000</pubDate>
		<dc:creator>Sanjeev Mishra</dc:creator>
				<category><![CDATA[Metaprograming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://sanjusoftware.wordpress.com/?p=12</guid>
		<description><![CDATA[The very hot and controversial topic I would say in the dynamic language world is Metaprograming. I am saying controversial, coz if not used properly it may lead to too many magics happening all across your application and it might become a nightmare to maintain it. Definition : &#8220;The phenomenon of a piece of code [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=12&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The very hot and controversial topic I would say in the dynamic language world is Metaprograming. I am saying controversial, coz if not used properly it may lead to too many magics happening all across your application and it might become a nightmare to maintain it.</p>
<p><strong>Definition </strong>: &#8220;The phenomenon of a piece of code writing another piece of code on the fly at the run time is metaprograming&#8221;</p>
<p>Definition seems to be very simple, but the implementation is not that simple, at least for me <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I am just introduced to this (metaprogramming) world. Learnt about it, because I started following <a title="Dave Thomas" href="http://pragdave.pragprog.com/" target="_blank">Dave Thomas</a>, the pragmatic programmer. I watched the seven videos presented by him about the ruby metaprgramming, mixins, bindings, lamda, procs etc. I would say the videos are worth watching and so is the book <a title="Agile Web Development With Rails" href="http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition" target="_blank">Agile web Development with Rails</a> by him. It&#8217;s very well written !! I must say.</p>
<p>So, I got exited after the videos and the book and couldn&#8217;t stop myself from penning down a few words about, what made me mad the most :  &#8220;Metaprograming&#8221;.</p>
<p>To start with lemme ask you a question &#8220;<strong>Have you ever wondered how come the things like att_reader or attr_accessor allow you to access the attributes of a particular modle in Ruby?</strong>&#8220;. Although Ruby as a language says that <strong>all the attributes of a model are inherently private</strong>. For example, let&#8217;s say I have a class</p>
<p>class Vehicle<br />
attr_accessor :no_of_wheels<br />
def initialize(no_of_wheels)<br />
@no_of_wheels = no_of_wheels<br />
end<br />
end</p>
<p>vehicle = Vehicle.new(2)<br />
puts &#8220;The vehicle has #{vehicle.no_of_wheels} wheels&#8221;<br />
vehicle.no_of_wheels = 4<br />
puts &#8220;The vehicle has #{vehicle.no_of_wheels} wheels&#8221;</p>
<p>Running the above program gives you the following output :</p>
<p>The vehicle has 2 wheels<br />
The vehicle has 4 wheels</p>
<p>How is that possible!!! If you are a JAVA guy (like me), you will definately start pulling your hair when this is working inspite of not finding any getter or setter defind for the same in the class hirarchy any where. What is this magic. How come I am not getting the error on accessing the private instance variable @no_of_wheels in the Vehicle class. Hmmm&#8230; thats insane.</p>
<p>Here is Metaprogramming coming to play. In fact, what just happened here is that as soon as you say attr_accessor :instance_var_name, ruby inserts the gettetrs and setters as two instance methods on the fly in the Vehicle class for you. so in effect that single line of code makes the ruby class look like this:</p>
<p>class Vehicle<br />
def initialize(no_of_wheels)<br />
@no_of_wheels = no_of_wheels<br />
end<br />
def no_of_wheels() #getter<br />
@no_of_wheels<br />
end<br />
def no_of_wheels=(no_of_wheels) #setter<br />
@no_of_wheels = no_of_wheels<br />
end<br />
end</p>
<p><strong>So at the run time, the no_of_wheels is available on the instances of the Vehicle class!! This is metaprogramming!</strong></p>
<p>Now this also doesn&#8217;t happen out of the thin air. The code for the attr_accessor is present in Module class an is implemented in such a way that it generates the getters and setters for you.</p>
<p>OK, let&#8217;s do this. Let&#8217;s write our own implementation of the attribuet accessors. You will find mostly this example lying around all over the internet and the reason is probably that this is the most simply explained and makes most sense as the ruby newbie&#8217;s, like me, already see a lot of&#8217;em lying around as soon as they start with this monster language.</p>
<p>So here we go. In this example I&#8217;ll try to open the class Module and add my attribute accessor to it. Let&#8217;s call our attribute accessor as : <strong>access_vars</strong></p>
<p>class Module<br />
def getter_setter_for(*vars)<br />
vars.each do |var|<br />
class_eval<br />
&#8220;def #{var}&#8221;<br />
&#8220;@#{var}&#8221;<br />
&#8220;end&#8221;<br />
&#8220;def #{m}=(val)&#8221;<br />
&#8220;@#{m}&#8221; = val&#8221;<br />
&#8220;end&#8221;<br />
end<br />
end<br />
end</p>
<p>And now in our class we can just say :</p>
<p>class Vehicle<br />
getter_setter_for :no_of_wheels<br />
def initialize(no_of_wheels)<br />
@no_of_wheels = no_of_wheels<br />
end<br />
end</p>
<p>And this will just get us the getters and setters inserted on the fly when the class is loaded.<br />
So here, <strong>the one line of code getter_setter_for or attr_accessor can write code on the fly. This is what metaprograming is!!</strong></p>
<p>That&#8217;s it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjusoftware.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjusoftware.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjusoftware.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjusoftware.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjusoftware.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjusoftware.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjusoftware.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjusoftware.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjusoftware.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjusoftware.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjusoftware.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjusoftware.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjusoftware.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjusoftware.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjusoftware.wordpress.com&amp;blog=1665638&amp;post=12&amp;subd=sanjusoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjusoftware.wordpress.com/2009/05/25/ruby-metaprograming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f184e469de246590a225b041bb1bead9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanju</media:title>
		</media:content>
	</item>
	</channel>
</rss>
