<?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>non-cohesive, tightly coupled</title>
	<atom:link href="http://dolzhenko.org/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://dolzhenko.org/blog</link>
	<description>Evgeniy Dolzhenko&#039;s blog on software related matters</description>
	<lastBuildDate>Sat, 03 Jul 2010 12:31:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Django F() And Q() Objects For Rails</title>
		<link>http://dolzhenko.org/blog/2010/07/django-f-and-q-objects-for-rails/</link>
		<comments>http://dolzhenko.org/blog/2010/07/django-f-and-q-objects-for-rails/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 12:31:39 +0000</pubDate>
		<dc:creator>Evgeniy Dolzhenko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://dolzhenko.org/blog/?p=226</guid>
		<description><![CDATA[
  
    Recently I was browsing 
    
      Django 
    documentation deliberately looking for
    some ideas to cross-pollinate to Rails. F() and Q() objects looked like
    nice little hacks so I decided to implement [...]]]></description>
			<content:encoded><![CDATA[<div>
  <p>
    Recently I was browsing 
    <a href="http://docs.djangoproject.com/en/1.2/">
      Django</a> 
    documentation deliberately looking for
    some ideas to cross-pollinate to Rails. F() and Q() objects looked like
    nice little hacks so I decided to implement them for Rails.
  </p>

  <span id="more-226"></span>

  <h2>Q() Object</h2>

  <p>
    Even though it&#8217;s not nearly as powerful as his Star Trek namesake 
    it&#8217;s still pretty interesting to look at (original docs available
    <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects">
      here</a>).
    It tackles the problem of complex SQL conditions building.
    Usually most issues arise from the fact that simple hash based conditions
    don&#8217;t allow for negated or <code>OR</code>ed conditions. There are plenty of
    other projects aimed to mitigate these restrictions among others (a while
    ago I assembled the collection of these):
    <a href="http://github.com/thoughtbot/squirrel">Squirrel</a>,
    <a href="http://github.com/defunkt/ambition">Ambition</a>,
    <a href="http://github.com/knowtheory/dm-sugar-glider">dm-sugar-glider</a>,
    <a href="http://github.com/ezmobius/ez-where">ez_where</a>,
    <a href="http://github.com/johnbender/rquery">RQuery</a>,
    <a href="http://github.com/binarylogic/searchlogic">Searchlogic</a>,
    <a href="http://github.com/jeremyevans/sequel">Sequel</a>,
    probably most recent one is
    <a href="http://github.com/ernie/meta_where">MetaWhere</a>.
  </p>

  <p style="clear: left;">
    There are different tricks above libraries employ
    <ol>
      <li>
        <code>instance_eval</code> and/or <code>method_missing</code> letting you
        write
        <pre class="brush: ruby;">
        # Squirrel
        Playlist.find(:all) do
          any do
            name == &quot;Party Mix&quot;
            total_length &gt; 3600
          end
        end
        </pre>
        or
        <pre class="brush: ruby;">
        # RQuery
        User.where do |user|
          (user.age &gt; 20) | (user.age.in 16,18)
        end
        </pre>
      </li>
      <li>core objects extensions
        (extending <code>Hash</code> and/or <code>Symbol</code> objects)
        <pre class="brush: ruby;">
        # Sequel
        Artist.filter(:name.like('Y%') &amp; ({:b=&gt;1} | ~{:c=&gt;3}))
        # SELECT * FROM artists WHERE name LIKE 'Y%' AND (b = 1 OR c != 3)
        </pre>
      </li>
      <li>
        hooking the interpreter (accessing syntax tree of the condition
        specifying block)
        <pre class="brush: ruby;">
        # Ambition
        &gt;&gt; SQL::User.select { |m| m.name == 'jon' &amp;&amp; m.age == 21 }.to_s
        =&gt; &quot;SELECT * FROM users WHERE users.name = 'jon' AND users.age = 21&quot;
        </pre>
      </li>
    </ol>
  </p>

  <p>
    While there is nothing technically wrong with the approaches listed above,
    it&#8217;s interesting to see what can be done without resorting to any of these
    tricks. The Q() object fits there very well. It let&#8217;s you build ORed,
    ANDed, and negated conditions without dropping to writing SQL (drawing
    on simple and well known hash-based syntax). Below is the illustration
    of Q() based syntax and the corresponding SQL fragments generated by it

    <pre class="brush: ruby;">
    ~Q(:user_id =&gt; nil) # =&gt; user_id IS NOT NULL

    Q(:user_id =&gt; nil) | Q(:id =&gt; nil) # =&gt; user_id IS NULL OR id IS NULL

    ~(Q(:user_id =&gt; nil) &amp; Q(:id =&gt; nil)) # =&gt; user_id IS NOT NULL AND id IS NOT NULL
    </pre>

    Ruby 1.9 allows <code>!</code> operator overloading leading to more
    natural syntax for negated conditions

    <pre class="brush: ruby;">
    !Q(:user_id =&gt; nil) # =&gt; user_id IS NOT NULL
    </pre>
  </p>

  <h2>F() Object</h2>

  <p>
    <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#query-expressions">
      Original docs</a>. It let&#8217;s you reference your database columns and do
    simple calculations on them from your conditions and update statements.
    The concept is pretty simple and can be illustrated with a few
    examples
    <pre class="brush: ruby;">
    # Simple column reference
    User.where(:updated_at =&gt; F(:created_at))
    # instead of writing it in SQL like: User.where(&quot;updated_at = created_at&quot;)

    # Column reference with calculation
    User.where(:updated_at =&gt; F(:created_at) + 1)
    # instead of User.where(&quot;updated_at = created_at + 1&quot;)

    # Calculation with references also work
    User.where(:updated_at =&gt; F(:created_at) + F(:updated_at))
    # instead of User.where(&quot;updated_at = created_at + updated_at&quot;)

    # Use in update statement
    User.update_all(:id_copy =&gt; F(:id))
    # instead of User.update_all(&quot;id_copy = id&quot;)
    </pre>

    Nice side effect of using this is that you get atomic operations for free
    <pre class="brush: ruby;">
      user = User.find(1)
      user.views = F(:views) + 1 #
      user.save
    </pre>
    
    Whenever the <code>save</code> is called it&#8217;s the current `views`
    that will be incremented (not the value that that was loaded with
    <code>User.find</code>).
  </p>

  <h2>Installation</h2>

  <p>
    You can get the code from my
    <a href="http://github.com/dolzenko/dolzenko-gem">
      eponymous gem</a>
    like this
    <pre class="brush: ruby;">
    &gt; gem install dolzenko
    &gt; irb
    ruby-head &gt; require 'dolzenko/django_q_object'
    ruby-head &gt; require 'dolzenko/django_f_object'
    </pre>
  </p>
  
</div>

]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2010/07/django-f-and-q-objects-for-rails/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Rails Pro Tip: Invoke Command Line Commands From Rails Console</title>
		<link>http://dolzhenko.org/blog/2010/07/rails-pro-tip-invoke-command-line-commands-from-rails-console/</link>
		<comments>http://dolzhenko.org/blog/2010/07/rails-pro-tip-invoke-command-line-commands-from-rails-console/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 12:21:48 +0000</pubDate>
		<dc:creator>Evgeniy Dolzhenko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://dolzhenko.org/blog/?p=216</guid>
		<description><![CDATA[
  
    So there you have your command line prompt from Bash/Zsh/cmd.exe and
    there is so called
    
      Rails console
    which is basically IRB prompt with the Rails application loaded.
  

  
    [...]]]></description>
			<content:encoded><![CDATA[<div>
  <p>
    So there you have your command line prompt from Bash/Zsh/cmd.exe and
    there is so called
    <a href="http://guides.rails.info/command_line.html#rails-console">
      Rails console</a>
    which is basically IRB prompt with the Rails application loaded.
  </p>

  <p>
    My tip suggests that you invoke some
    <a href="http://guides.rails.info/command_line.html">
      Rails commands</a>, namely generator related commands and rake tasks,
    from the running Rails console.
    The reason to do so is clear and simple &#8211; <b>it&#8217;s fast</b>.
  </p>
<span id="more-216"></span>
  <h2>Real World Proof</h2>

  <p>
    Let&#8217;s do some math to prove the point. The last project I was involved with
    contains 171 models, 158 controllers and 572 migrations. Models and
    controllers were mostly generated together by single
    <code>./script/generate scaffold</code> command. Migrations were generated
    with
    <code>./script/generate migration</code> followed by
    <code>rake db:migrate</code> command. This accounts for about 1300 command
    line invocations.
  </p>

  <p>
    Now sad statistics &#8211; running generator command from command line
    loads application environment which is not that cheap even on empty project
    <pre class="brush: ruby;">
    rails3_app/ &gt; /usr/bin/time -p rails generate migration just_testing
          invoke  active_record
          create    db/migrate/20100702124738_just_testing.rb
    real 7.54
    user 3.40
    sys 4.09
    </pre>
    On the real world project I&#8217;m using as an example this time (because of
    many gems and other dependencies) was <b>up to one minute</b>.
  </p>

  <p>
    On the other hand running the same command from Rails console (i.e. with
    environment already loaded) is <b>instantaneous</b> 
    <pre class="brush: ruby;">
    rails3_app/ &gt; rails console
    Loading development environment (Rails 3.0.0.beta4)
    MyIrb loaded
    ruby-head &gt; time { migration 'test9' }
          invoke  active_record
          create    db/migrate/20100702125251_test9.rb
    Time elapsed: 0.07000 user, 0.08000 system (0.14645 wall clock seconds)
    </pre>

    (assuming environment loading took 30 seconds on average 
    one can calculate the gain from using this technique for the whole project:
    <code>30 seconds * 1300 invocations/3600 = 10.8 hours</code> <img src='http://dolzhenko.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) 
  </p>

  <h2>Getting/Using It</h2>

  <p>
    Helpers that bring generator and rake commands to IRB prompt are available
    as part of my
    <a href="http://github.com/dolzenko/my_irb">IRB configuration</a>
    in
    <a href="http://github.com/dolzenko/my_irb/blob/master/my_irb/rails/console_in_console.rb">
      console_in_console.rb</a> file.

    Assuming it&#8217;s saved at <code>~/console_in_console.rb</code>
    
    <pre class="brush: ruby;">
    rails3_app/ &gt; rails c
    ruby-head &gt; load '~/console_in_console.rb'
    ruby-head &gt; include MyIrb::Rails::ConsoleInConsole
    # Have fun now generating and destroying migrations, models, controllers
    # much faster than before

    ruby-head &gt; model 'user login:string password:string'
          invoke  active_record
          create    db/migrate/20100702132016_create_users.rb
          create    app/models/user.rb
          invoke    test_unit
          create      test/unit/user_test.rb
          create      test/fixtures/users.yml
    
    ruby-head &gt; migration 'migration_name'
          invoke  active_record
          create    db/migrate/20100702130723_migration_name.rb

    ruby-head &gt; destroy_migration 'migration_name'
          invoke  active_record
          remove    db/migrate/20100702130723_migration_name.rb

    ruby-head &gt; rake 'db:migrate'
    ==  Test11: migrating =========================================================
    ==  Test11: migrated (0.0000s) ================================================
    </pre>

    And it works with Rails 2.x branch too!
  </p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2010/07/rails-pro-tip-invoke-command-line-commands-from-rails-console/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Safe String Interpolation In Ruby</title>
		<link>http://dolzhenko.org/blog/2010/07/safe-string-interpolation-in-ruby/</link>
		<comments>http://dolzhenko.org/blog/2010/07/safe-string-interpolation-in-ruby/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 08:10:02 +0000</pubDate>
		<dc:creator>Evgeniy Dolzhenko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://dolzhenko.org/blog/?p=202</guid>
		<description><![CDATA[
  Age-Old Problem

  
    Suppose you want to authenticate user from the login/password entered
    on the web from
    
    user = User.first(:conditions =&#62; &#34;login = '#{ login }' AND password = '#{ password }'&#34;)
    
    [...]]]></description>
			<content:encoded><![CDATA[<div>
  <h2>Age-Old Problem</h2>

  <p>
    Suppose you want to authenticate user from the login/password entered
    on the web from
    <pre class="brush: ruby;">
    user = User.first(:conditions =&gt; &quot;login = '#{ login }' AND password = '#{ password }'&quot;)
    </pre>
    Such authentication code as we know can be easily fooled with the
    <a href="http://en.wikipedia.org/wiki/SQL_injection">SQL injection</a>.

    Thankfully in Rails there are parameterized queries and the above can be
    rewritten in at least 3 injection safe ways
    <pre class="brush: ruby;">
    User.find_by_login_and_password(login, password)
    # or
    User.first(:conditions =&gt; [&quot;login = ? AND password = ?&quot;, login, password])
    # or
    User.first(:conditions =&gt; [&quot;login = :login AND password = :password&quot;, { :login =&gt; login, :password =&gt; password }])
    </pre>
  </p>

  <h2>Many Solutions (Haskell Included)</h2>

  <p>
    Parameterized queries cover most of the cases but what if you have large
    query with multiple parameters? 
    You can quickly lose track of all the question marks and order
    in which parameters appear. Most likely you will prefer the latter,
    hash-based notation.
  </p>

  <span id="more-202"></span>

  <p>
    Side note: This stuff is actually analyzed in depth by
    <a href="http://onestepback.org/">
      Jim Weirich</a>
    in his
    <a href="http://mwrc2009.confreaks.com/14-mar-2009-18-10-the-building-blocks-of-modularity-jim-weirich.html">
      The Building Blocks of Modularity</a>
    presentation where he talks about the connascence in software.
  </p>

  <p>
    Back to our topic what we really want is just to write SQL queries
    injecting properly quoted values where needed. In other words we would like
    to be able to customize the way string interpolation works. In cool
    languages like Haskell this concept is natively supported and called
    <a href="http://www.haskell.org/haskellwiki/Quasiquotation">
      Quasiquotation</a>. 
    While not really on par with such rigorous achievements it can
    be simulated everywhere where <code>eval</code> is available.
    Here is for example
    <a href="http://google-caja.googlecode.com/svn/changes/mikesamuel/string-interpolation-29-Jan-2008/trunk/src/js/com/google/caja/interp/index.html">
      the solution</a>
    from the
    <a href="http://en.wikipedia.org/wiki/Caja_(programming_language)">
      Google Caja</a>
    team.
  </p>

  <h2>Hack For Better Future</h2>

  <p>
    There is <code>eval</code> in Ruby and there is also one peculiar feature
    about it. You can pass <code>binding</code> to it, and when the
    block is created it&#8217;s <code>binding</code> is captured and therefore can be
    used from anywhere (providing access to the lexical scope captured by the
    block). Let&#8217;s illustrate that
    <pre class="brush: ruby;">
    ruby-head &gt; def m
                  local = 42
                  proc {} # return empty block to get binding from it later
                end
    ruby-head &gt; eval(&quot;local&quot;, m.binding) # binding gives access to the scope where block was created
    =&gt; 42 
    </pre>
  </p>

  <p>
    Using that feature we can hide <code>eval</code> and implement our own
    safe interpolation routines like I did in
    <a href="http://github.com/dolzenko/dolzenko-gem/blob/master/lib/dolzenko/safe_interpolate.rb">
      safe_interpolate.rb</a>.
    Use it like
    
    <pre class="brush: ruby;">
    ruby-head &gt; require 'dolzenko/safe_interpolate'
    ruby-head &gt; include SafeInterpolate
    ruby-head &gt; login = &quot;' or 1=1&quot;
    ruby-head &gt; sql_interpolate { 'login = #{ login }' }
     =&gt; &quot;login = ''' or 1=1'&quot; # single quote got turned into double and the whole expression is quoted itself
    </pre>

    HTML and URI escaping are also provided
    <pre class="brush: ruby;">
    ruby-head &gt; content = &quot;&lt;script&gt;alert(1)&lt;/script&gt;&quot;
    ruby-head &gt; html_interpolate { '&lt;p&gt;#{ content }&lt;/p&gt;' }
    =&gt; &quot;&lt;p&gt;&amp;lt;script&amp;gt;alert(1)&amp;lt;/script&amp;gt;&lt;/p&gt;&quot; # injection prevented!

    ruby-head &gt; query = &quot;&amp;admin=1&quot;
    ruby-head &gt; uri_interpolate { 'http://example.com?q=#{ query }' }
    =&gt; &quot;http://example.com?q=%26admin%3D1&quot;
    </pre>
  </p>

  <h2>Further Reading</h2>

  <p>
    <a href="http://blog.moertel.com/articles/2006/10/18/a-type-based-solution-to-the-strings-problem">
      A type-based solution to the &#8220;strings problem&#8221;: a fitting end to XSS and SQL-injection holes?</a>
    by Tom Moertel. This article got me interested in the subject. <br />
    
    <a href="http://intoverflow.wordpress.com/2010/06/30/haskell-features-id-like-to-see-in-other-languages/">
      Haskell features I&#8217;d like to see in other languages</a>
    by Tim Carstens. Check the &laquo;Quasi-quoting&raquo; section for collection of Haskell
    quoting related links.
  </p>
</div>]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2010/07/safe-string-interpolation-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Travel To The Core Rails 3 Methods Without Leaving IRB Prompt</title>
		<link>http://dolzhenko.org/blog/2010/05/travel-to-the-core-rails-3-methods-without-leaving-irb-prompt/</link>
		<comments>http://dolzhenko.org/blog/2010/05/travel-to-the-core-rails-3-methods-without-leaving-irb-prompt/#comments</comments>
		<pubDate>Fri, 28 May 2010 07:31:07 +0000</pubDate>
		<dc:creator>Evgeniy Dolzhenko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dolzhenko.org/blog/?p=167</guid>
		<description><![CDATA[
  
    In the previous post I
    introduced Reflexive gem
    which lets you browse the classes and modules for running application with
    Web UI. But we&#8217;re really often just goofing off at the IRB prompt, aren&#8217;t
    we? For this [...]]]></description>
			<content:encoded><![CDATA[<div>
  <p>
    In the <a href="http://dolzhenko.org/blog/?p=150">previous post</a> I
    introduced <a href="http://github.com/dolzenko/reflexive">Reflexive</a> gem
    which lets you browse the classes and modules for running application with
    Web UI. But we&#8217;re really often just goofing off at the IRB prompt, aren&#8217;t
    we? For this case I have
    <a href="http://github.com/dolzenko/method_extensions">
      extracted some portions of Reflexive
    </a>
    to help with code navigation right from the
    interactive prompt.
  </p>

  <span id="more-167"></span>
  
  <p>
    Let&#8217;s see how these may help us look under the hoods of Rails 3 framework.
    <i>Disclaimer:</i> this posts is intended as the demonstration of
    navigation techniques that can be used on any code base, not necessary Ruby on
    Rails, also if you&#8217;re looking for detailed explanation of the architecture of particular Rails
    components <a href="http://mediumexposure.com">Maxim Chernyak</a> compiled
    <a href="http://mediumexposure.com/rails-3-reading-material/">Rails 3 Reading Material</a>
    list is the great place to start.
  </p>

  <p>
    <i>Side note:</i> it may look like things demonstrated below could be done with debugger and
    more accurately so. My idea is that debugger basically follows <b>push model</b> so
    program state is pushed to you and you just follow along.
    On the other hand navigating manually
    is more like a <b>pull model</b> which is much more engaging and more effective
    when you try to understand and remember patterns in foreign code (well at
    least that&#8217;s how it is for me <img src='http://dolzhenko.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )
  </p>

  <h2>ActiveRecord find method</h2>
  <p>
    Suppose we want to find out what happens when we call ActiveRecord
    <code>find</code> method.
    That&#8217;s the easy one. First install method_extensions gem, add it to <code>Gemfile</code>
    and start console
    <pre class="brush: ruby;">
    root@ubuntu:~/ &gt; rvm use ruby-1.9.2-head 
    root@ubuntu:~/ &gt; rvm gemset create rails3 &amp;&amp; rvm gemset use rails3 # sandbox our Rails 3 experiments in rails3 Gemset (h/t to Mark Carey for this note)
    root@ubuntu:~/ &gt; gem install rails --pre
    root@ubuntu:~/ &gt; gem install method_extensions
    root@ubuntu:~/ &gt; rails rails3_test_app
    root@ubuntu:~/ &gt; cd rails3_test_app
    root@ubuntu:~/rails3_test_app/ &gt; echo 'gem &quot;method_extensions&quot;' &gt;&gt; Gemfile
    root@ubuntu:~/rails3_test_app/ &gt; bundle install
    root@ubuntu:~/rails3_test_app/ &gt; rails c
    ruby-1.9.2-head &gt;
    </pre>
  </p>

  <p>
    Now ActiveRecord <code>find</code> is a class method so to retrieve
    corresponding <code>Method</code> object we&#8217;re using <code>method</code>
    method
    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; ActiveRecord::Base.method(:find)
    =&gt; #&lt;Method: ActiveRecord::Base.find&gt; 
    </pre>

    Let&#8217;s try to get method source with
    <code style="font-size: 16px;"><b>source</b></code> method

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActiveRecord::Base.method(:find).source
    ArgumentError: failed to find method definition around the lines:

      delegate :find, :first, :last, :all, :destroy, :destroy_all, :exists?, :delete, :delete_all, :update, :update_all, :to =&gt; :scoped
      delegate :find_each, :find_in_batches, :to =&gt; :scoped
    </pre>

  </p>

  <p>
    Not very impressive, but we can see at least that <code>find</code> is
    delegated method, and it&#8217;s delegated to the result of <code>scoped</code>
    method. That&#8217;s how <code>delegate</code> works, but what if we had no idea
    about <code>delegate</code> method and how it works?
  </p>

  <p>
    <code style="font-size: 16px;"><b>source_with_doc</b></code>
    method will also return the comment preceding the method definition
    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActiveRecord::Base.method(:delegate).source_with_doc
    # Provides a delegate class method to easily expose contained objects' methods
    # as your own. Pass one or more methods (specified as symbols or strings)
    # ... snip
    def delegate(*methods)
      options = methods.pop
      unless options.is_a?(Hash) &amp;&amp; to = options[:to]
        raise ArgumentError, &quot;Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to =&gt; :greeter).&quot;
      end
    # ... snip
    end
    </pre>
  </p>

  <p>
    Now that we know that <code>find</code> is delegated to <code>scoped</code>
    indeed, what does <code>scoped</code> return?
    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActiveRecord::Base.method(:scoped).source_with_doc
    # Returns an anonymous scope.
    # ... snip
    def scoped(options = {}, &amp;block)
      if options.present?
        relation = scoped.apply_finder_options(options)
        block_given? ? relation.extending(Module.new(&amp;block)) : relation
      else
        current_scoped_methods ? unscoped.merge(current_scoped_methods) : unscoped.clone
      end
    end
    </pre>
  </p>

  <p>
    Here we need to stop for a moment and consider what this piece of code may
    potentially do. We can see that in presence of <code>options</code> hash
    function recursive call is made without <code>options</code> passed which
    takes us to the second branch which ultimately calls <code>unscoped</code>.
    Let&#8217;s see what it does.

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActiveRecord::Base.method(:unscoped).source
    def unscoped
      @unscoped ||= Relation.new(self, arel_table)
      finder_needs_type_condition? ? @unscoped.where(type_condition) : @unscoped
    end
    </pre>

    <i>Plug for Reflexive</i>: if we want to get more context and have
    <a href="http://github.com/dolzenko/reflexive">Reflexive</a>
    installed there is the shorthand to generate link
    <a href="http://reflexive-demo.heroku.com/reflexive/constants/ActiveRecord::Base/class_methods/unscoped/definition#highlighted">like this one</a>
    (which shows the whole file where the method is defined, can lookup constants
    and some method calls and is otherwise neat <img src='http://dolzhenko.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )
    
    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; ActiveRecord::Base.method(:unscoped).reflexive_url
    =&gt; &quot;http://localhost:3000/reflexive/constants/ActiveRecord::Base/class_methods/unscoped&quot; 
    </pre>
  </p>

  <p>
    Rewinding a little back to the <code>scoped</code> definition we see that <code>find</code>
    is in the end called on an instance of <code>ActiveRecord::Relation</code>.
    Let&#8217;s confirm that using <code style="font-size: 16px;"><b>full_inspect</b></code>
    method also added by <code>method_extensions</code> gem. It is just the
    plain <code>inspect</code>, <code>source_location</code>, and
    <code>source_with_doc</code> put together

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActiveRecord::Relation.instance_method(:find).full_inspect
    #&lt;UnboundMethod: ActiveRecord::Relation(ActiveRecord::FinderMethods)#find&gt;
    [&quot;~/rails3_test_app/vendor/rails/activerecord/lib/active_record/relation/finder_methods.rb&quot;, 90]
    # Find operates with four different retrieval approaches:
    # ... snip
    def find(*args, &amp;block)
      return to_a.find(&amp;block) if block_given?
      # ... snip
    end
    </pre>

    So we just got information on the class hierarchy (plain <code>inspect</code>
    on the first line says that <code>FinderMethods</code> is
    mixed-in into <code>Relation</code>), the place where method is defined and
    method code with documentation. Not that bad I think considering we didn&#8217;t
    have to go to the code directly (either on the web or in editor). Now we can
    move to something slightly more complicated.
  </p>

  <h2>ActionController render method</h2>

  <p>
    Let&#8217;s get into it

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:render).full_inspect
    #&lt;UnboundMethod: ActionController::Base(ActionController::Instrumentation)#render&gt;
    [&quot;~/rails3_test_app/vendor/rails/actionpack/lib/action_controller/metal/instrumentation.rb&quot;, 36]
    def render(*args)
      render_output = nil
      self.view_runtime = cleanup_view_runtime do
        Benchmark.ms { render_output = super }
      end
      render_output
    end
    </pre>

    D&#8217;oh! Not much again, what we get is generic code from
    <code>ActionController::Instrumentation</code> used to measure view
    rendering time. In other words &#8211; boring stuff and all it looks like the
    whole job is handled in <code>super</code>. But how we get there?
  </p>

  <p>
    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; require &quot;method_extensions/method/super&quot;
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:render).super.source_with_doc
    # Check for double render errors and set the content_type after rendering.
    def render(*args) #:nodoc:
      raise ::AbstractController::DoubleRenderError if response_body
      super
      self.content_type ||= Mime[formats.first].to_s
      response_body
    end
    </pre>

    <code>require "method_extensions/method/super"</code> adds
    <code style="font-size: 16px;"><b>super</b></code>
    method but since it does so in a hacky/core methods monkey-patching way
    it&#8217;s not loaded by default. <code>super</code> supports chaining we need in
    this case since we&#8217;re not getting the real rendering action yet

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:render).super.super.full_inspect
    # Normalize arguments, options and then delegates render_to_body and
    # sticks the result in self.response_body.
    def render(*args, &amp;block)
      self.response_body = render_to_string(*args, &amp;block)
    end
    </pre>

    So what we actually need is <code>render_to_string</code>. Now just keep going
    using the <code>super</code> trick where needed

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:render_to_string).source_with_doc
    # Raw rendering of a template to a string. Just convert the results of
    # render_to_body into a String.
    # :api: plugin
    def render_to_string(*args, &amp;block)
      options = _normalize_args(*args, &amp;block)
      _normalize_options(options)
      render_to_body(options)
    end
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:render_to_body).source_with_doc
    def render_to_body(options)
      options[:template].sub!(/^\//, '') if options.key?(:template)
      super || &quot; &quot;
    end
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:render_to_body).super.source_with_doc
    def render_to_body(options)
      _handle_render_options(options) || super
    end
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:render_to_body).super.super.source_with_doc
    # Raw rendering of a template to a Rack-compatible body.
    # :api: plugin
    def render_to_body(options = {})
      _process_options(options)
      _render_template(options)
    end
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:_render_template).source_with_doc
    # Find and renders a template based on the options given.
    # :api: private
    def _render_template(options) #:nodoc:
      view_context.render(options)
    end
    </pre>

    All that to find out that render is basically delegated to the
    <code>view_context</code>. But we&#8217;re already comfortable with that thing
    and can proceed further

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:view_context).source_with_doc
    # An instance of a view class. The default view class is ActionView::Base
    #
    # The view class must have the following methods:
    # View.new[lookup_context, assigns, controller]
    #   Create a new ActionView instance for a controller
    # View#render[options]
    #   Returns String with the rendered template
    #
    # Override this method in a module to change the default behavior.
    def view_context
      view_context_class.new(lookup_context, view_assigns, self)
    end
    ruby-1.9.2-head &gt; puts ActionController::Base.instance_method(:view_context_class).source_with_doc
    def view_context_class
      @view_context_class || self.class.view_context_class
    end
    </pre>

    And finally we&#8217;re there

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActionController::Base.method(:view_context_class).source_with_doc
    def view_context_class
      @view_context_class ||= begin
        controller = self
        Class.new(ActionView::Base) do
          if controller.respond_to?(:_helpers)
            include controller._helpers

            if controller.respond_to?(:_router)
              include controller._router.url_helpers
            end

            # TODO: Fix RJS to not require this
            self.helpers = controller._helpers
          end
        end
      end
    end
    </pre>

    This method illustrates how helpers declared by controller and routing
    helpers (methods like <code>post_url</code>, etc. generated from routes)
    are made available in views. We also get the source and options documentation
    for <code>render</code> method

    <pre class="brush: ruby;">
    ruby-1.9.2-head &gt; puts ActionView::Base.instance_method(:render).source_with_doc
    # Returns the result of a render that's dictated by the options hash. The primary options are:
    #
    # * &lt;tt&gt;:partial&lt;/tt&gt; - See ActionView::Partials.
    # * &lt;tt&gt;:update&lt;/tt&gt; - Calls update_page with the block given.
    # * &lt;tt&gt;:file&lt;/tt&gt; - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
    # * &lt;tt&gt;:inline&lt;/tt&gt; - Renders an inline template similar to how it's done in the controller.
    # * &lt;tt&gt;:text&lt;/tt&gt; - Renders the text passed in out.
    def render(options = {}, locals = {}, &amp;block)
      case options
      # ... snip
    end
    </pre>
  </p>

  <p>
    While this won&#8217;t replace proper docs/debugger in any way it was nice to
    see how self-reflection capabilities of Ruby can be improved with just a few
    hacky helper methods. Hopefully the travel wasn&#8217;t too boring <img src='http://dolzhenko.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ,
    comments/suggestions/patches are welcome indeed!
  </p>

<p><i>Added note:</i> I&#8217;ve just started using this for real and it holds up pretty well so I decided to add some shortcuts to my <code>.irbrc</code>
and <code>method_extensions</code> which will make the IRB print
result of <code>full_inspect</code> verbatim by default. Check 
<a href="http://github.com/dolzenko/method_extensions/blob/master/lib/method_extensions/method/source_with_doc.rb#L114">this method and comment</a>
</div>]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2010/05/travel-to-the-core-rails-3-methods-without-leaving-irb-prompt/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Reflexive: Live Class And Source Code Browser</title>
		<link>http://dolzhenko.org/blog/2010/05/reflexive-live-class-and-source-code-browser/</link>
		<comments>http://dolzhenko.org/blog/2010/05/reflexive-live-class-and-source-code-browser/#comments</comments>
		<pubDate>Thu, 20 May 2010 16:45:33 +0000</pubDate>
		<dc:creator>Evgeniy Dolzhenko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dolzhenko.org/blog/?p=150</guid>
		<description><![CDATA[  Background

  Recently I was trying to understand how the
  Arel (Relational Algebra for Ruby)
  internals work.

  The project has strong OO design and uses some metaprogramming techniques.
  Main code base consists of ~ 80 files, with files having 30
  LOCs on average.

  I&#8217;m totally cool with [...]]]></description>
			<content:encoded><![CDATA[  <h3>Background</h3>

  <p>Recently I was trying to understand how the
  <a href="http://github.com/rails/arel">Arel (Relational Algebra for Ruby)</a>
  internals work.</p>

  <p>The project has strong OO design and uses some metaprogramming techniques.
  Main code base consists of ~ 80 files, with files having 30
  <span title="lines of code">LOCs</span> on average.</p>

  <p>I&#8217;m totally cool with that kind of modular/small classes design, but for new
  comer (like me) it could be a little daunting and I had to draw some diagrams
  on paper before finally <i>getting it</i> instead of <i>getting lost</i> in
  inheritance chains, compositional patterns and so on.</p>

  <h3>Solved In SmallTalk 20 Years Ago</h3>

  <p>
    <a href="http://dolzhenko.org/blog/wp-content/uploads/2010/05/Browser2.gif">
      <img src="http://dolzhenko.org/blog/wp-content/uploads/2010/05/Browser2_thumb.gif" alt="Squeak Class Browser"/>
    </a>
  </p>
  <p>Indeed as with many other problems it&#8217;s been solved in SmallTalk for many years.
  Having live classes reside in live virtual machine is one the SmallTalk features
  that makes rich reflection facilities possible and which is missing from Ruby.</p>

  <h3>So Can We Have A VM Please?</h3>

  <p>While there is no Ruby VM in a strict sense what&#8217;s that Rails development server
  process we have running if not a VM with classes kept in sync with their &laquo;on the
  disk&raquo;/plain-text representation? Well at least it&#8217;s pretty close, also the
  fact that at least half of the metaprogramming stuff happens at load time
  definitely helps for better reflections (think <code>klass.included</code> hook,
  <code>attr_reader</code>, <code>delegate</code> methods)</p>

  <h3>What IDE?</h3>

  <p>Really I would love to have something like this built-in into my favorite IDE,
  but the problem is that I&#8217;m not even sure what&#8217;s <i>mine</i> favorite IDE is,
  and there is even less agreement on the subject in the community.</p>

  <h3>What Ruby Offers? Or Quick Recap Of Ruby Reflection Techniques</h3>

  <ul>
    <li>
      <a href="http://ruby-doc.org/core/classes/Object.html#M000336">Kernel#method</a>
      and
      <a href="http://ruby-doc.org/core/classes/Module.html#M001659">Kernel#instance_method</a>
      let&#8217;s you determine where the method defined on an object (instance of the object)
      comes from
      (<a href="http://gist.github.com/76951">an old trick taught by Dave Thomas</a>).
      Less known is that with 1.9 you can get the file and line number of method
      definition with
      <a href="http://www.ruby-doc.org/core-1.9/classes/Method.html#M000222">Method#source_location</a>
    </li>
    <li>
      <a href="http://ruby-doc.org/core-1.9/classes/Module.html#M000077">Module#ancestors</a>
      because of Ruby unified object model let&#8217;s you determine class ancestors,
      modules included in class, and modules included by another module. This
      also works seamlessly for singleton classes (i.e. when you have something
      like <code>C.extend(M)</code> module <code>M</code> becomes an ancestor
      of <code>C.singleton_class</code>)
    </li>
    <li>
      <a href="http://ruby-doc.org/core-1.9/classes/Object.src/M000178.html">Object#methods</a>
      and
      <a href="http://ruby-doc.org/core-1.9/classes/Module.html#M000082">Module#instance_methods</a>
      when passed false as first parameter can be used to enumerate methods owned
      by class (as contrasted with returning owned and inherited methods by default)
    </li>
    <li>
      <a href="http://github.com/shyouhei/ruby/tree/trunk/ext/ripper">Ripper Ruby parser</a>
      although not directly reflection related Ripper is part of a standard library
      and is based on <code>parse.y</code> (the grammar file which Ruby interpreter
      uses) which automatically makes it always up-to-date with any Ruby syntax
      changes.
    </li>
  </ul>
  
  <h3>Get Reflexive</h3>

  <p>With the set of aforementioned tools in hand I went and created sort of
  proof-of-concept good old web-app with virtually no UI and enjoyed the process
  a great deal. It browses classes and modules, and the corresponding source
  code files with some interactivity twists (and a little dose of pink color).</p>

  <p>Visit the <a href="http://github.com/dolzenko/reflexive">GitHub project page</a>
  to learn more about what it does and how to get
  it for your applications. <br/>
  You can also check out the
  <a href="http://reflexive-demo.heroku.com/reflexive/dashboard">
    demo Rails 3 app with Reflexive installed
  </a> running on Heroku but be warned that Heroku has old Ruby 1.9.1 which has
  some issues with the reflection capabilities Reflexive uses.</p>

  <h3>In Conclusion</h3>

  <p>It seems to me that live reflection is the really powerful way to get precise code
  navigation/autocompletion/refactoring tools in Ruby IDEs. It also should be
  possible to integrate Reflexive in RDoc or YARD to get more interactive source
  code listings, but I scratched my own itch now and have to move on <img src='http://dolzhenko.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2010/05/reflexive-live-class-and-source-code-browser/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exploring Latest ruby-lib Additions</title>
		<link>http://dolzhenko.org/blog/2010/03/exploring-latest-ruby-lib-additions/</link>
		<comments>http://dolzhenko.org/blog/2010/03/exploring-latest-ruby-lib-additions/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 11:58:35 +0000</pubDate>
		<dc:creator>Evgeniy Dolzhenko</dc:creator>
				<category><![CDATA[main]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://dolzhenko.org/blog/?p=85</guid>
		<description><![CDATA[Recently I was browsing through the NEWS file in the Ruby Subversion repository. Two methods added since the 1.9.1 release to the Enumerable module caught my attention. These two are Enumerable#chunk and Enumerable#slice_before.

The docs for these are not available at http://ruby-doc.org/ so you&#8217;d need to go straight to the source code (by using the links [...]]]></description>
			<content:encoded><![CDATA[Recently I was browsing through the <a href="http://github.com/shyouhei/ruby/blob/trunk/NEWS">NEWS</a> file in the Ruby Subversion repository. Two methods added since the 1.9.1 release to the Enumerable module caught my attention. These two are <a href="http://github.com/shyouhei/ruby/blob/trunk/enum.c#L2221"><code>Enumerable#chunk</code></a> and <a href="http://github.com/shyouhei/ruby/blob/trunk/enum.c#L2396"><code>Enumerable#slice_before</code></a>.

The docs for these are not available at <a href="http://ruby-doc.org/">http://ruby-doc.org/</a> so you&#8217;d need to go straight to the source code (by using the links above). The source code comments have a few usage examples but I wanted to expand on that, since I feel like they are if not crucial but certainly very useful additions to the set of excellent Enumerable/Array methods Ruby already provides.

From this post you can learn how to:
<ul>
<li><a href="#chunk">Use Enumerable#chunk to recreate Facebook &#8220;Show N Similar Posts&#8221; feature</a></li>
<li><a href="#slice_before">Use Enumerable#slice_before to easily parse mysqldump, Rails log, and INI files</a></li>
<li><a href="#structure_aware_log_grep">Tail Rails log files like a pro targeting only specific actions/clients</a></li>
</ul>

<h2 id="chunk">Enumerable#chunk</h2>
Using this method you can <em>chunk</em> consecutive elements of the array into the sub-arrays based on some property calculated for the element by the passed block. The returned array has <code>Array#assoc</code> semantics, i.e. the first value of each sub-array is the property on which the chunk was selected and the second is the actual array of consecutive elements having this property.

Suppose we&#8217;re implementing something like this Facebook feature:
<img class="alignnone size-full wp-image-91" title="show_similar_posts" src="http://dolzhenko.org/blog/wp-content/uploads/2010/03/show_similar_posts.png" alt="" width="374" height="115" />

Here similar updates from the same user are collapsed so that you get less spam from your overly active friends. For the sake of demonstration we can assume that there are no special similarity rules and all updates from the same user are considered similar, assuming that you have <code>Update</code> model which stores the update related info (like who did what and on which date) you can chunk the similar updates together using the following code in controller:

<pre class="brush: ruby;">
class UpdatesController
  def index
    # since we're not interested in the property we're chunking the array on (user here),
    # we get just the chunks themselves with +map(&amp;:second)+
    @chunked_updates = Update.order(&quot;created_at DESC&quot;).chunk(&amp;:user).map(&amp;:second)
  end
end
</pre>

* Note this example requires Rails 3 and Ruby 1.9.2 to run properly

And in the corresponding view:

<pre class="brush: ruby; html-script: true;">
&lt;!-- views/updates/index.html.erb --&gt;
&lt;% @chunked_updates.each do |update_chunk| %&gt;
  &lt;%= render update_chunk.first %&gt;
  &lt;% if update_chunk.size &gt; 1 %&gt;
    Show &lt;%= pluralize update_chunk.size - 1, &quot;similar update&quot; %&gt;
    &lt;!-- ... render additional updates here ... --&gt;
  &lt;% end %&gt;
&lt;% end %&gt;
</pre>

<h2 id="slice_before">Enumerable#slice_before</h2>
This method is particularly when parsing files with the flat structure like:
<pre><code>
Section 1 Header
Section 1 contents
...
Section 2 Header
Section 2 contents
...
</code></pre>
Using <code>slice_before</code> you can extract such sections/slices. Turns out there are quite a few places where this could be useful:
<ul>
	<li><a href="http://gist.github.com/327027" target="_blank">INI files. Gist which parses INI file into Hash</a></li>
	<li><a href="http://gist.github.com/327085" target="_blank">mysqldump files. Gist which extracts particular tables from whole database dump</a></li>
	<li><a href="http://gist.github.com/327022" target="_blank">Rails application log files. Gist which calculates cumulative running time per action</a></li>
</ul>
But probably the most useful of them is the following.

<h2 id="structure_aware_log_grep">Structure aware log grep</h2>
Suppose you&#8217;ve got a production Rails app running with multiple clients/search engines making requests simultaneously. Now you need to investigate the issue with one particular client, or you want to monitor requests hitting some particular controller. Doing <code>tail -f log/production.log</code> outputs a lot of requests you&#8217;re not interested in. You can pipe the output to <code>grep</code>, but then you&#8217;d get one line per request, while the whole request log consists of multiple lines.

One solution would be to use <code>--context</code> grep option which will show fixed amount of lines around the line where the match occurred, but the problem is that the amount of context lines is fixed while the amount of lines in request log may vary (for example if exception was thrown there will be the whole stacktrace, while for normal request there will be only about 3-4 lines).

More robust solution would be to parse the log file into &#8220;slices&#8221; on the fly and show only ones you&#8217;re interested in. <a href="http://gist.github.com/327785" target="_blank">Here is the gist</a> and some usage examples:
<pre><code>
Show only local requests: 
tail -f log/production.log | grep_rails_log_file.rb 127.0.0.1

Show only GET requests: 
tail -f log/production.log | grep_rails_log_file.rb GET

Show only requests to /updates path: 
tail -f log/production.log | grep_rails_log_file.rb /updates
</code></pre>

<h2>Use it right now</h2>
And the best part is that you can start using all the goodness right now even if your project is running on older Ruby version. The excellent <a href="http://github.com/marcandre/backports">Backports Library</a> by <a href="http://blog.marc-andre.ca/">Marc-André Lafortune</a> already has these methods backported. Usually I don&#8217;t include the whole library but just rip out the needed method and place it under <code>lib/core_ext</code> folder.]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2010/03/exploring-latest-ruby-lib-additions/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title></title>
		<link>http://dolzhenko.org/blog/2009/05/72/</link>
		<comments>http://dolzhenko.org/blog/2009/05/72/#comments</comments>
		<pubDate>Tue, 12 May 2009 18:47:33 +0000</pubDate>
		<dc:creator>dolzenko</dc:creator>
				<category><![CDATA[main]]></category>

		<guid isPermaLink="false">http://dolzenko.wordpress.com/?p=72</guid>
		<description><![CDATA[The YUI home page is frying my brains.


  
    JavaScript library
    Links from the home page
  
  
    The Yahoo! User Interface Library (YUI)
    137
  
  
    jQuery
    45
  
  [...]]]></description>
			<content:encoded><![CDATA[The <a href="http://developer.yahoo.com/yui/">YUI</a> home page is frying my brains.

<table>
  <tr>
    <th>JavaScript library</th>
    <th>Links from the home page</th>
  </tr>
  <tr>
    <td><a href="http://developer.yahoo.com/yui/">The Yahoo! User Interface Library (YUI)</a></td>
    <td>137</td>
  </tr>
  <tr>
    <td><a href="http://jquery.com/">jQuery</a></td>
    <td>45</td>
  </tr>
  <tr>
    <td><a href="http://www.prototypejs.org/">Prototype</a></td>
    <td>15</td>
  </tr>
</table>

Damn, what&#8217;s so incredibly hard with doing less?]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2009/05/72/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ultimate YAML in IRb and all the think alikes</title>
		<link>http://dolzhenko.org/blog/2009/05/ultimate-yaml-in-irb-and-all-the-think-alikes/</link>
		<comments>http://dolzhenko.org/blog/2009/05/ultimate-yaml-in-irb-and-all-the-think-alikes/#comments</comments>
		<pubDate>Mon, 11 May 2009 18:34:43 +0000</pubDate>
		<dc:creator>dolzenko</dc:creator>
				<category><![CDATA[main]]></category>

		<guid isPermaLink="false">http://dolzenko.wordpress.com/?p=68</guid>
		<description><![CDATA[After I realized that I want to switch from default inspect output in IRB to YAML output, I googled and found this Secrets of the Rails Console Ninjas from Amy Hoy which suggested to use y method, then Geoffrey Grosenbach suggests class Object; def inspect; y self; end; end, but that would break for explicitly [...]]]></description>
			<content:encoded><![CDATA[After I realized that I want to switch from default <code>inspect</code> output in IRB to YAML output, I googled and found this <a href="http://slash7.com/articles/2006/12/21/secrets-of-the-rails-console-ninjas">Secrets of the Rails Console Ninjas</a> from Amy Hoy which suggested to use <code>y</code> method, then Geoffrey Grosenbach <a href="http://slash7.com/articles/2006/12/21/secrets-of-the-rails-console-ninjas#comment_13430">suggests</a> <code>class Object; def inspect; y self; end; end</code>, but that would break for explicitly defined inspect&#8217;s, oh no! Evil-mokey-patch to the rescue:
<pre lang="ruby">
IRB.CurrentContext.irb.instance_eval do
  def output_value
    printf @context.return_format, y(@context.last_value)
  end
end
</pre>]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2009/05/ultimate-yaml-in-irb-and-all-the-think-alikes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title></title>
		<link>http://dolzhenko.org/blog/2009/04/65/</link>
		<comments>http://dolzhenko.org/blog/2009/04/65/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 06:32:52 +0000</pubDate>
		<dc:creator>dolzenko</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://dolzenko.wordpress.com/2009/04/30/65/</guid>
		<description><![CDATA[Finally gave in and started to manage my dotfiles through github. Wasted me an hour or so to setup, but hey http://github.com/dolzenko/dotfiles/tree/master]]></description>
			<content:encoded><![CDATA[Finally gave in and started to manage my dotfiles through github. Wasted me an hour or so to setup, but hey <a href="http://github.com/dolzenko/dotfiles/tree/master">http://github.com/dolzenko/dotfiles/tree/master</a>]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2009/04/65/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>too smart rubies</title>
		<link>http://dolzhenko.org/blog/2009/04/too-smart/</link>
		<comments>http://dolzhenko.org/blog/2009/04/too-smart/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 17:16:53 +0000</pubDate>
		<dc:creator>dolzenko</dc:creator>
				<category><![CDATA[main]]></category>

		<guid isPermaLink="false">http://dolzenko.wordpress.com/?p=61</guid>
		<description><![CDATA[
s = ""
s ]]></description>
			<content:encoded><![CDATA[<pre lang="ruby">
s = ""
s << 0 # just great
s << 255 # still cool
s << 256 # `<<': can't convert Fixnum into String (TypeError)
</pre>
Now imagine that the null character comes from the variable and only under certain conditions. And the string then passed further to some component which just happily assumes the string ended at that null character.

And I never liked that ASCII art method anyway. Probably wouldn't ever use it but many languages still suck at string concatenation.]]></content:encoded>
			<wfw:commentRss>http://dolzhenko.org/blog/2009/04/too-smart/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

