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

<channel>
	<title>A Place for my head &#187; old_posts</title>
	<atom:link href="http://gnufied.org/category/old_posts/feed/" rel="self" type="application/rss+xml" />
	<link>http://gnufied.org</link>
	<description>On Ruby, Rails, Concurrency and fiction</description>
	<lastBuildDate>Mon, 10 Oct 2011 06:49:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using class instance variables in ruby</title>
		<link>http://gnufied.org/2007/10/24/using-class-instance-variables-in-ruby/</link>
		<comments>http://gnufied.org/2007/10/24/using-class-instance-variables-in-ruby/#comments</comments>
		<pubDate>Thu, 25 Oct 2007 03:25:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[old_posts]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gnufied.org/?p=3</guid>
		<description><![CDATA[We all know how evil class variables are, and they are as dangerous as the “three headed dog” at the dungeon and we shall not talk about it. But they are necessary for many of the thingies ruby does and used extensively.But today we shall not talk about them. We saw earlier that, although class [...]]]></description>
			<content:encoded><![CDATA[<p>We all know how evil class variables are, and they are as dangerous as the “three headed dog” at the dungeon and we shall not talk about it.<br />
But they are necessary for many of the thingies ruby does and used extensively.But today we shall not talk about them.<br />
We saw earlier that, although class instance variables are excellent, but not so friendly to use and they make Ruby look like C++(ahem).</p>
<p>Here goes a little hack, that allows you to define class level attributes based on class instance variables. Since, i often use it in rails and they have taken cattr for class level attributes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">metaclass</span>; <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>; <span style="color:#0000FF; font-weight:bold;">self</span>; <span style="color:#9966CC; font-weight:bold;">end</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">iattr_accessor</span> <span style="color:#006600; font-weight:bold;">*</span>args
&nbsp;
    metaclass.<span style="color:#9900CC;">instance_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      attr_accessor <span style="color:#006600; font-weight:bold;">*</span>args
      args.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>attr<span style="color:#006600; font-weight:bold;">|</span>
        define_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;set_#{attr}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>b_value<span style="color:#006600; font-weight:bold;">|</span>
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{attr}=&quot;</span>,b_value<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    args.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>attr<span style="color:#006600; font-weight:bold;">|</span>
      class_eval <span style="color:#9966CC; font-weight:bold;">do</span>
        define_method<span style="color:#006600; font-weight:bold;">&#40;</span>attr<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>attr<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        define_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{attr}=&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>b_value<span style="color:#006600; font-weight:bold;">|</span>
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{attr}=&quot;</span>,b_value<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Now you can easily write code like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foobar
  iattr_accessor <span style="color:#ff3333; font-weight:bold;">:hello</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Foobar.<span style="color:#9900CC;">hello</span> = <span style="color:#996600;">&quot;I am a class instance atrribute&quot;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> Foobar.<span style="color:#9900CC;">hello</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://gnufied.org/2007/10/24/using-class-instance-variables-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

