<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Ruby Blender</title>
	
	<link>http://blender.matthewkwilliams.com</link>
	<description />
	<lastBuildDate>Tue, 01 Sep 2009 19:42:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RubyBlender" /><feedburner:info uri="rubyblender" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Default values for Attributes</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/akK49aQbXrM/</link>
		<comments>http://blender.matthewkwilliams.com/2009/09/01/default-values-for-attributes/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 19:42:00 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=82</guid>
		<description><![CDATA[This is the first in a series of entries which I&#8217;m pulling over articles from an old blog, revising, etc&#8230; The Problem One of the neat things about Ruby is it&#8217;s ability to create accessor methods for you, by simply declaring attr_reader, attr_writer, or attr_accessor.&#160; However, there&#8217;s not an easy way to define a default [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first in a series of entries which I&#8217;m pulling over articles from an old blog, revising, etc&#8230;</p>
<h3>The Problem</h3>
<p>One of the neat things about <a class="zem_slink" title="Ruby (programming language)" rel="homepage" href="http://www.ruby-lang.org/">Ruby</a> is it&#8217;s ability to create accessor methods for you, by simply declaring <code>attr_reader</code>, <code>attr_writer</code>, or <code>attr_accessor</code>.&nbsp; However, there&#8217;s not an easy way to define a default value for an attribute.&nbsp;&nbsp;The original version of this code was based off of <a href="http://www.railsonwave.com/2008/4/15/create-getter-and-setter-on-a-valorized-variable/">Create getter and setter on a valorized variable</a> and ideas from Ruby Quiz #67: <a href="http://www.rubyquiz.com/quiz67.html">Metakoans</a>.&nbsp; The main difference between it and  Sandro Paganotti&#8217;s version is that you can pass in a block which will be evaluated each time it is invoked (in case the value might depend on something else).&nbsp; Additionally, it will work with <a class="zem_slink" title="Boolean datatype" rel="wikipedia" href="http://en.wikipedia.org/wiki/Boolean_datatype">boolean</a> types.</p>
<h3>The Solution</h3>
<p><code lang="ruby">class Object<br />
  def self.attribute(*arg,&#038;block)<br />
    (name, default) = arg<br />
    short_name = name.to_s.sub(/\?/,"")<br />
    self.send(:define_method, name) {<br />
      if instance_variables.include? "@#{short_name}"<br />
           self.instance_eval "@#{short_name}"<br />
      else<br />
        if block_given?<br />
          instance_eval &#038;block<br />
        else<br />
          default<br />
        end<br />
      end<br />
    }<br />
    self.send(:define_method, "#{short_name}="){ |value|<br />
      self.instance_eval "@#{short_name} = value"<br />
    }<br />
  end<br />
end<br />
</code></p>
<p>&nbsp;</p>
<p>In order to use it, you could do something like this:</p>
<p><code lang="ruby">class Foo<br />
  attribute :bar<br />
  attribute(:fud) {instance_variables.include?("@bar") ? @bar : ' '}<br />
  attribute :fi, 10<br />
  attribute :flag?, true<br />
end<br />
</code></p>
<p>And, in action, it would look like this:</p>
<p><code lang="ruby">>> f=Foo.new<br />
=> #<Foo:0x2ad8c6af7728><br />
>> f.bar # defined, but not set to anything (nil)<br />
=> nil<br />
>> f.fud # since bar is not defined, return an empty space<br />
=> " "<br />
>> f.fi # just return the default<br />
=> 10<br />
>> f.flag? # and here's a boolean<br />
=> true<br />
>> f.bar = 5 # now we set bar<br />
=> 5<br />
>> f.fud # Since bar is set, then fud will return its value<br />
=> 5<br />
>> f.fud = 20 # now we explicitly set fud, and the block is no longer used<br />
=> 20<br />
>> f.fud # fud returns 20 as expected<br />
=> 20<br />
>> f.bar # and, as expected, changing fud has no effect on bar<br />
=> 5<br />
</code></p>
<p>&nbsp;</p>
<p>There might be a better way to do this; but one of the things I love about Ruby is that you can add new features to the language very easily.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=d92dc226-b9b3-47d9-9981-7eeb4013d521" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/akK49aQbXrM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/09/01/default-values-for-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/09/01/default-values-for-attributes/</feedburner:origLink></item>
		<item>
		<title>Arguments with Trollop</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/hRyKxsuAbrI/</link>
		<comments>http://blender.matthewkwilliams.com/2009/08/21/arguments-with-trollop/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 15:11:31 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubygems]]></category>
		<category><![CDATA[Command-line interface]]></category>
		<category><![CDATA[Library]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=78</guid>
		<description><![CDATA[Command line arguments can be a hassle to parse.  Trollop makes them easier.]]></description>
			<content:encoded><![CDATA[<p>At various points in my life, I&#8217;ve used many methods of parsing command line options, from rolling my own, to using getopt or other libraries.&nbsp; Recently I came across a new one which has its own view of how to parse commandline arguments.</p>
<p><a href="http://trollop.rubyforge.org/">Trollop</a> is, as they put it, &#8220;Yet Another Fine CommandLine Argument Parsing Library&#8221; (YAFCLAP).&nbsp; It&#8217;s goal is to be as easy as possible.&nbsp; You can install it as a single file in <code>lib</code>, or use it as a gem.&nbsp; Trollop doesn&#8217;t care.&nbsp; To install, you can download it from rubyforge, or use the ever popular:</p>
<p><code lang="bash">gem install trollop</code></p>
<p>(you may need to prepend <code>sudo</code>, or run it as root).</p>
<p>Let&#8217;s take a look at a simple example:</p>
<p><code lang="ruby">require '<a class="zem_slink" title="RubyGems" rel="homepage" href="http://docs.rubygems.org">rubygems</a>'<br />
require 'trollop'<br />
opts = Trollop::options do<br />
  opt :host, "Host to connect to", :default => "localhost"<br />
  opt :port, "jmx remote port", :default => 4444<br />
  opt :interval, "How often do we poll (in seconds)", :default => 10<br />
  opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0<br />
end<br />
p opts<br />
</code></p>
<p>&nbsp;</p>
<p>The first argument to opt is the name of the option.&nbsp; We don&#8217;t have to specify a switch; it&#8217;s smart enough to figure out a mapping for them.&nbsp; The second is a description of the argument.&nbsp; You can specify a default value and/or a type.&nbsp; In this example, we&#8217;ve specified defaults.&nbsp; And here it is in use:</p>
<p><code lang="bash">$ ruby trollop_simple.rb<br />
{:interval=>10, :count=>0, :help=>false, :host=>"localhost", :port=>4444}<br />
$ ruby trollop_simple.rb -h<br />
Error: option '-h' needs a parameter.<br />
Try --help for help.<br />
$ ruby trollop_simple.rb --help<br />
Options:<br />
      --host, -h <s>:   Host to connect to (default: localhost)<br />
      --port, -p <i>:   jmx remote port (default: 4444)<br />
  --interval, -i <i>:   How often do we poll (in seconds) (default: 10)<br />
     --count, -c <i>:   How many intervals, 0 == infinite, use ^C (SIGINT) to<br />
                        exit and write output (default: 0)<br />
          --help, -e:   Show this message</p>
<p></code></p>
<p>Notice that it has set up mappings for us as well as created a nicely formatted help &#8212; though it&#8217;s a default, without a lot of information.&nbsp;&nbsp; Let&#8217;s add a version and usage:</p>
<p><code lang="ruby">require 'rubygems'<br />
require 'trollop'<br />
opts = Trollop::options do<br />
  version <<EOF<br />
jmx_mon -- we be jammin' with version zz9-za aka Arthur Dent<br />
Don't forget your towel!<br />
EOF</p>
<p>  banner <<EOF<br />
jmx_mon is a program to monitor a jmx bean\'s value over time.<br />
It can output the data as a csv, a graph in a pdf, or both.</p>
<p>Usage jmx_mon [options]<br />
where [options] are:<br />
EOF<br />
  opt :host, "Host to connect to", :default => "localhost"<br />
  opt :port, "jmx remote port", :default => 4444<br />
  opt :interval, "How often do we poll (in seconds)", :default => 10<br />
  opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0<br />
end<br />
p opts<br />
</code></p>
<p>Now let&#8217;s see what we have:</p>
<p><code lang="bash">$ ruby trollop_simple.rb --help<br />
jmx_mon is a program to monitor a jmx bean's value over time.<br />
It can output the data as a csv, a graph in a pdf, or both.</p>
<p>Usage jmx_mon [options]<br />
where [options] are:<br />
      --host, -h <s>:   Host to connect to (default: localhost)<br />
      --port, -p <i>:   jmx remote port (default: 4444)<br />
  --interval, -i <i>:   How often do we poll (in seconds) (default: 10)<br />
     --count, -c <i>:   How many intervals, 0 == infinite, use ^C (SIGINT) to<br />
                        exit and write output (default: 0)<br />
       --version, -v:   Print version and exit<br />
          --help, -e:   Show this message</p>
<p>$ ruby trollop_simple.rb -v<br />
jmx_mon -- we be jammin' with version zz9-za aka Arthur Dent<br />
Don't forget your towel!</p>
<p></code></p>
<p>We now have a nicer usage, as well as version.&nbsp; Also, it will stop automagickally if we ask for help or the version.</p>
<p>All of the options are placed in the opts hash; let&#8217;s see what we can do with it:</p>
<p><code lang="ruby">require 'rubygems'<br />
require 'trollop'</p>
<p>aliases = {<br />
      :MEMORY_HEAP => "Heap memory usage, multiple values",<br />
      :MEMORY_HEAP_USED => "Used heap memory"<br />
}</p>
<p>opts = Trollop::options do<br />
  version <<EOF<br />
jmx_mon -- we be jammin' with version zz9-za aka Arthur Dent<br />
Don't forget your towel!<br />
EOF</p>
<p>  banner <<EOF<br />
jmx_mon is a program to monitor a jmx bean\'s value over time.<br />
It can output the data as a csv, a graph in a pdf, or both.</p>
<p>Usage jmx_mon [options]<br />
where [options] are:<br />
EOF<br />
  opt :host, "Host to connect to", :default => "localhost"<br />
  opt :port, "jmx remote port", :default => 4444<br />
  opt :interval, "How often do we poll (in seconds)", :default => 10<br />
  opt :count, "How many intervals, 0 == infinite, use ^C (SIGINT) to exit and write output", :default => 0<br />
  opt :aliases, "Print defined bean aliases &#038; exit.", :default => false<br />
  opt :towel, "Don't forget this!", :default => false<br />
end</p>
<p>if opts[:aliases_given]<br />
  aliases.each_pair do |key, value|<br />
    puts "#{key}: #{value}"<br />
  end<br />
  exit<br />
end</p>
<p>Trollop::die :towel, "You forgot your towel.  You've been eaten by a Ravenous Bugblatter Beast of Traal" unless opts[:towel]</p>
<p>p opts<br />
</code></p>
<p>We&#8217;ve added two more options, one for printing out aliases, and another indicating whether or not you have your towel &#8212; with deadly results should you forget.</p>
<p><code lang="bash">$ ruby trollop_simple.rb -a<br />
MEMORY_HEAP: Heap memory usage, multiple values<br />
MEMORY_HEAP_USED: Used heap memory</p>
<p>$ ruby trollop_simple.rb<br />
Error: argument --towel You forgot your towel.  You've been eaten by a Ravenous Bugblatter Beast of Traal.<br />
Try --help for help.</p>
<p></code></p>
<p>There&#8217;s more that you can do with trollop; the website and documentation gives more examples.&nbsp; However, I&#8217;d suggest considering trollop the next time you need to parse the command line.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=7aad43fc-5a6d-4cd5-a19b-87519b5c2bdc" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/hRyKxsuAbrI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/08/21/arguments-with-trollop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/08/21/arguments-with-trollop/</feedburner:origLink></item>
		<item>
		<title>Database Paranoia — ActiveRecord Callbacks</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/lVr6fILx7ww/</link>
		<comments>http://blender.matthewkwilliams.com/2009/08/20/database-paranoia-activerecord-callbacks/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 16:27:52 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=75</guid>
		<description><![CDATA[On a particular, nameless, heterogenous project using ruby on rails, ant, and shell scripts, there was an instance where data was not being properly cleaned before going into the database (primarily due to user errors in property files which were used by ant to feed the database).&#160;&#160; As a result, the database was filled with [...]]]></description>
			<content:encoded><![CDATA[<p>On a particular, nameless, heterogenous project using <a class="zem_slink" title="Ruby on Rails" rel="homepage" href="http://rubyonrails.org/">ruby on rails</a>, ant, and shell scripts, there was an instance where data was not being properly cleaned before going into the database (primarily due to user errors in property files which were used by ant to feed the database).&nbsp;&nbsp; As a result, the database was filled with extraneous spaces which were causing many issues.&nbsp; A perfect illustration of <a class="zem_slink" title="Garbage In, Garbage Out" rel="wikipedia" href="http://en.wikipedia.org/wiki/Garbage_In%2C_Garbage_Out">GIGO</a>.</p>
<p>One problem was that there were multiple databases, each with their own data.&nbsp;&nbsp; By the time the issue was discovered, a fix was needed soonest, so the solution was less than elegant, but it does illustrate the use of callbacks in <a class="zem_slink" title="ActiveRecord (Castle)" rel="wikipedia" href="http://en.wikipedia.org/wiki/ActiveRecord_%28Castle%29">ActiveRecord</a>:</p>
<p><code lang="ruby">module DbParanoia<br />
# This is a really stupid thing to have to back-fit; it is making<br />
# sure, however, that the users enter good data.  We ensure that<br />
# strings are stripped on read and write.</p>
<p>  def trim_space<br />
    self.attributes.each do |attr|<br />
      attr.strip! if attr.instance_of? String<br />
    end<br />
  end</p>
<p>  alias_method :after_find, :trim_space<br />
  alias_method :before_save, :trim_space<br />
end<br />
</code></p>
<p>&nbsp;</p>
<p>The above module is using the after_find and before_save callbacks to make sure that:</p>
<ol>
<li>Any spaces coming back from the database are trimmed.</li>
<li>Any new records and/or updates will have the spaces trimmed.</li>
</ol>
<p>You can use this method to perform pretty much any sort of validation or data manipulation of the data of an ActiveRecord class.&nbsp; Other callbacks include:</p>
<ul>
<li>after_create</li>
<li>after_destroy</li>
<li>after_save</li>
<li>after_update</li>
<li>after_validation</li>
<li>after_validation_on_create</li>
<li>after_validation_on_update</li>
<li>before_create</li>
<li>before_destroy</li>
<li>before_save</li>
<li>before_update</li>
<li>before_validation</li>
<li>before_validation_on_create</li>
<li>before_validation_on_update</li>
</ul>
<p>You can read more about ActiveRecord::Callbacks at <a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html">http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html</a>.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=32531c0f-5714-4592-8a9c-073b5100fecd" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/lVr6fILx7ww" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/08/20/database-paranoia-activerecord-callbacks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/08/20/database-paranoia-activerecord-callbacks/</feedburner:origLink></item>
		<item>
		<title>Ruby Bindings and Scope</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/iz_UoyWAmlo/</link>
		<comments>http://blender.matthewkwilliams.com/2009/08/19/ruby-bindings-and-scope/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 21:06:33 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[Local variable]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=71</guid>
		<description><![CDATA[One of the features of Ruby&#8217;s Kernel module (which is imported into Object, so it&#8217;s available in every class) is the binding.&#160; The binding allows you to keep a pointer to the scope at a particular location in your application and then later evaluate code from that location. For example (and this came up as [...]]]></description>
			<content:encoded><![CDATA[<p>One of the features of Ruby&#8217;s Kernel module (which is imported into Object, so it&#8217;s available in every class) is the binding.&nbsp; The binding allows you to keep a pointer to the scope at a particular location in your application and then later evaluate code from that location.</p>
<p>For example (and this came up as the result of a <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/344180">question</a> on the ruby talk&nbsp; mailing list), one of the things you can do is obtain a list of the local variables of a method:</p>
<p><code lang="ruby">def my_vars(a)<br />
  b=a<br />
  c=b+a<br />
  puts local_variables<br />
end</code></p>
<p>&nbsp;</p>
<p>returns the three variables, a, b, and c.&nbsp; That&#8217;s fine, we&#8217;re still within the scope of the method.&nbsp; But how about outside of our method?&nbsp; This is where #binding and the Binding class come into play:</p>
<p><code lang="ruby">def outside(b)<br />
  puts eval("local_variables", b)<br />
end</p>
<p>def my_vars(a)<br />
  b=a<br />
  c=b+a<br />
  outside(binding)<br />
end</code></p>
<p>&nbsp;</p>
<p>One thing to note: in order to work with the binding, we have to #eval code within its scope.&nbsp; Thus the call to #eval with the method we are invoking and the binding.&nbsp; This leads to the question from the mailing list:</p>
<blockquote><p>we already have #has_block? to see if a block was passed. So how about<br />
a #has_arguments? to query if _any_ arguments have been passed &#8212; <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/344180">Intransition</a></p></blockquote>
<p>So how would we go about writing a #has_arguments? &#8212; here is a first stab:</p>
<p><code lang="ruby">class Object<br />
  def has_arguments?(b)<br />
    vars = eval("local_variables",b)<br />
    return false if vars.length == 0<br />
    vars.each do |v|<br />
      return false if eval("#{v}.nil?",b)<br />
    end<br />
    return true<br />
  end<br />
end</p>
<p>class Foo<br />
  def initialize(first=nil)<br />
    if (has_arguments? binding)<br />
      puts "We're defined"<br />
    else<br />
      puts "Not!"<br />
    end<br />
  end<br />
end</code></p>
<p>The only real drawback here is that we&#8217;ve got to pass in the binding itself.&nbsp; Let&#8217;s try again:</p>
<p><code lang="ruby">class Object<br />
  def has_arguments?(&#038;b)<br />
    vars = eval("local_variables",b.binding)<br />
    return false if vars.length == 0<br />
    vars.each do |v|<br />
      return false if eval("#{v}.nil?",b)<br />
    end<br />
    return true<br />
  end<br />
end</p>
<p>class Foo<br />
  def initialize(first=nil)<br />
    if (has_arguments? {}) # note the empty block<br />
      puts "We're defined"<br />
    else<br />
      puts "Not!"<br />
    end<br />
  end<br />
end</code></p>
<p>In this case, instead of explicitly calling #binding, we&#8217;re passing in an empty block &#8212; the block has it&#8217;s own scope, but it happens to contain the scope of then enclosing method.&nbsp; It&#8217;s still not perfect, however:</p>
<ol>
<li>You need to pass in an empty block.</li>
<li>It fails if you have local variables other than those passed in as arguments. (Pointed out by <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/344265">Joel VanderWerf</a>)</li>
</ol>
<p>Ok, it&#8217;s got some serious flaws (Charles Nutter posted a different way of <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/344267">solving</a> the problem, which does work for all cases, and doesn&#8217;t use #binding).&nbsp; But it does give an idea of what you can do with #binding.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=226ed4f0-8ead-4e3e-b046-04359bd0ae8b" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/iz_UoyWAmlo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/08/19/ruby-bindings-and-scope/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/08/19/ruby-bindings-and-scope/</feedburner:origLink></item>
		<item>
		<title>What’s #method_missing missing?</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/hD0j50769C4/</link>
		<comments>http://blender.matthewkwilliams.com/2009/08/18/whats-method_missing-missing/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 18:57:54 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=67</guid>
		<description><![CDATA[#method_missing is a useful feature in ruby.  However, there's something that many people miss when using it.]]></description>
			<content:encoded><![CDATA[<p>#method_missing is a very useful part of the <a class="zem_slink" title="Ruby (programming language)" rel="homepage" href="http://www.ruby-lang.org/">ruby language</a>.&nbsp; However, there&#8217;s one common mistake that developers make when using it.&nbsp; They forget to add a call to #super.</p>
<p>Let&#8217;s take a look:</p>
<p><code lang="ruby">$ irb<br />
>> class Foo<br />
>>   end<br />
=> nil<br />
>> f=Foo.new<br />
=> #<Foo:0xb75122fc><br />
>> f.i_do_not_exist<br />
NoMethodError: undefined method `i_do_not_exist' for #<Foo:0xb75122fc><br />
 from (irb):5<br />
>> class Foo<br />
>>   def method_missing(symbol,*args)<br />
>>     if (symbol.to_s === "i_do_not_exist")<br />
>>       puts "Yes, actually you do"<br />
>>       end<br />
>>     end<br />
>>   end<br />
=> nil<br />
>> f.i_do_not_exist<br />
Yes, actually you do<br />
=> nil<br />
>> f.i_exist<br />
=> nil</code></p>
<p>Unfortunately, we no longer get the error when we try to invoke a method which does not exist.&nbsp; When we add an invocation to super, it&#8217;ll behave as expected:</p>
<p><code lang="ruby">>> class Foo<br />
  def method_missing(symbol,*args)<br />
    if (symbol.to_s === "i_do_not_exist")<br />
      puts "Yes, actually you do"<br />
    else<br />
      super<br />
    end<br />
  end<br />
end<br />
>>   >>     >>       >>       ?>         >>       >>     >>   => nil<br />
>> f.i_exist<br />
NoMethodError: undefined method `i_exist' for #<Foo:0xb75122fc><br />
 from (irb):31:in `method_missing'<br />
 from (irb):35<br />
>> f.i_do_not_exist<br />
Yes, actually you do<br />
=> nil<br />
>> </code></p>
<p>&nbsp;</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=8932e6ed-06b0-4874-b604-9b9af7c6ffd4" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/hD0j50769C4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/08/18/whats-method_missing-missing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/08/18/whats-method_missing-missing/</feedburner:origLink></item>
		<item>
		<title>After long silence</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/s8oBMmt3OB4/</link>
		<comments>http://blender.matthewkwilliams.com/2009/04/10/after-long-silence/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 17:26:57 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/2009/04/10/after-long-silence/</guid>
		<description><![CDATA[The hiatus has been due to me having been really sick with a weird neurological disorder.&#160; I didn&#8217;t do any coding at all for over 2 months.&#160; I&#8217;m starting to feel better, but I still have my good days and bad.&#160; So, there will be updates.&#160; Hopefully before not too much longer.]]></description>
			<content:encoded><![CDATA[<p>The hiatus has been due to me having been really sick with a weird neurological disorder.&nbsp; I didn&#8217;t do any coding at all for over 2 months.&nbsp; I&#8217;m starting to feel better, but I still have my good days and bad.&nbsp; So, there will be updates.&nbsp; Hopefully before not too much longer.</p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" src="http://img.zemanta.com/pixy.gif?x-id=d7796f09-badf-84e7-ba70-2c546b3fdf71" /></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/s8oBMmt3OB4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/04/10/after-long-silence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/04/10/after-long-silence/</feedburner:origLink></item>
		<item>
		<title>Overriding operators: two dimensional arrays</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/ANwgMyR0nQ8/</link>
		<comments>http://blender.matthewkwilliams.com/2009/02/23/overriding-operators-two-dimensional-arrays/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 21:53:32 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=58</guid>
		<description><![CDATA[Image via Wikipedia One of the neat things about ruby is that, in general, operators are methods, so they can be overridden. When working with multidimensional arrays, some languages allow you to do the following: a[0,0] whereas ruby uses: a[0][0] Which makes sense, since Ruby implements it as an array of arrays and you&#8217;re chaining [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignright" style="width: 212px;">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:Array_of_array_storage.svg"><img title="A two-dimensional array stored as a one-dimens..." src="http://upload.wikimedia.org/wikipedia/commons/thumb/0/01/Array_of_array_storage.svg/202px-Array_of_array_storage.svg.png" alt="A two-dimensional array stored as a one-dimens..." width="202" height="145" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:Array_of_array_storage.svg">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>One of the neat things about ruby is that, in general, operators are methods, so they can be overridden.  </p>
<p>When working with multidimensional arrays, some languages allow you to do the following:</p>
<p><code lang="text">a[0,0]</code></p>
<p>whereas ruby uses:</p>
<p><code lang="ruby">a[0][0]</code></p>
<p>Which makes sense, since Ruby implements it as an array of arrays and you&#8217;re chaining methods to get to the element.  But, since we can override the [] method, we can then say a[0,0].</p>
<p><code lang="ruby">class Grid<br />
  attr_reader :cols, :rows</p>
<p>  # initialize our grid and optionally prepopulate it.<br />
  # klass can behave in the following fashions:<br />
  # nil -- nil<br />
  # a value (like "", 0, true) -- that value<br />
  # a class which responds to new -- an instance of that class<br />
   def initialize(rows=1, cols=1, klass = nil)<br />
    @rows = rows<br />
    @cols = cols<br />
    @grid = (0 ... @rows).collect do |y|<br />
      (0 ... @cols).collect do |x|<br />
        if klass.nil? then<br />
          nil<br />
        elsif klass.respond_to? :new<br />
          klass.new<br />
        else<br />
          klass<br />
        end<br />
      end<br />
    end<br />
  end</p>
<p># This method allows us to say +grid[x, y]+ instead of +grid[y][x]+<br />
  def [](x, y=nil)<br />
    (x,y) = x if x.instance_of? Array<br />
    @grid[y][x]<br />
  end</p>
<p>  # This method allows us to set the value of a grid grid entry via<br />
  # +grid[x, y]+ instead of +grid[y][x]+<br />
  def []=(*args)<br />
    if (args[0].instance_of? Array)<br />
      ((x, y),value) = args<br />
    else<br />
      (x,y,value) = args<br />
    end<br />
    @grid[y][x] = value<br />
  end</p>
<p>end</code></p>
<p>So, it can be used like this:</p>
<p><code lang="text">$ irb<br />
>> require 'grid'<br />
=> true<br />
>> g=Grid.new<br />
=> #< Grid:0xb747d760 @cols=1, @rows=1, @grid=[[nil]] ><br />
>> g=Grid.new(2,2)<br />
=> #< Grid:0xb747bb04 @cols=2, @rows=2, @grid=[[nil, nil], [nil, nil]] ><br />
>> g=Grid.new(2,2,0)<br />
=> #< Grid:0xb7479aac @cols=2, @rows=2, @grid=[[0, 0], [0, 0]] ><br />
>> g=Grid.new(2,2,String)<br />
=> #< Grid:0xb7477810 @cols=2, @rows=2, @grid=[["", ""], ["", ""]] ><br />
>> g=Grid.new(2,2,false)<br />
=> #< Grid:0xb74755d8 @cols=2, @rows=2, @grid=[[false, false], [false, false]] ><br />
>> g[0,0]<br />
=> false<br />
>> g[0,0]=true<br />
=> true<br />
>> g<br />
=> #< Grid:0xb74755d8 @cols=2, @rows=2, @grid=[[true, false], [false, false]] ></code></p>
<p>It&#8217;s an interesting technique &#8212; and given that most people think of arrays/coordinates in pairs, I think that it can make the code more &#8220;readable&#8221;.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=30343586-84a3-448c-aba1-0d2582ae77c6" alt="Enhanced by Zemanta" /></a></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/ANwgMyR0nQ8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/02/23/overriding-operators-two-dimensional-arrays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/02/23/overriding-operators-two-dimensional-arrays/</feedburner:origLink></item>
		<item>
		<title>To Paren, or not to Paren</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/3qOfa4ZPZV0/</link>
		<comments>http://blender.matthewkwilliams.com/2009/02/20/to-paren-or-not-to-paren/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 19:11:53 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[paradigm]]></category>
		<category><![CDATA[philosophy]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=54</guid>
		<description><![CDATA[To paren, or not to paren: that is the question: Whether &#8217;tis nobler in the mind to suffer The bugs and errors of maintenance programs, Or to take arms against a host of typos, And by debugging end them? To hack: to slash; No more; and by a slash to say we end The heart-ache [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>To paren, or not to paren: that is the question:</p>
<p>Whether &#8217;tis nobler in the mind to suffer</p>
<p>The bugs and errors of  maintenance programs,</p>
<p>Or to take arms against a host of typos,</p>
<p>And by <a class="zem_slink" title="Debugging" rel="wikipedia" href="http://en.wikipedia.org/wiki/Debugging">debugging</a> end them?  To hack: to slash;</p>
<p>No more; and by a slash to say we end</p>
<p>The heart-ache and the thousand natural shocks</p>
<p>Of <a class="zem_slink" title="Cut, copy, and paste" rel="wikipedia" href="http://en.wikipedia.org/wiki/Cut%2C_copy%2C_and_paste">cut and paste</a> code, &#8217;tis a maturation</p>
<p>Devoutly to be wished.  To hack, not slash;</p>
<p>Not slash: perchance to code: ay, there&#8217;s the rub;</p>
<p>For in that new programme what code may come</p>
<p>When we have shuffled off this unix box,</p>
<p>Must give us pause:  there&#8217;s the respect</p>
<p>That makes wuffy of so long life;</p>
<p>&#8211; Not <a class="zem_slink" title="William Shakespeare" rel="lastfm" href="http://www.last.fm/music/William%2BShakespeare">William Shakespeare</a></p></blockquote>
<p>There&#8217;s been a good deal of conversation of late on comp.lang.ruby as to whether or not to use <a class="zem_slink" title="Parenthesis (rhetoric)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Parenthesis_%28rhetoric%29">parentheses</a>.  And when to use them.  <a class="zem_slink" title="Ruby (programming language)" rel="homepage" href="http://www.ruby-lang.org/">Ruby</a> is such that parentheses are optional, except, of course, when they&#8217;re not.  Here&#8217;s some examples of where they are not:</p>
<p><code lang="text">irb(main):002:0&gt; "parenthesis".length()<br />
=&gt; 11<br />
irb(main):003:0&gt; "parenthesis".length<br />
=&gt; 11</p>
<p>...</p>
<p>irb(main):009:0&gt; class <a class="zem_slink" title="Foo" rel="wikipedia" href="http://en.wikipedia.org/wiki/Foo">Foo</a><br />
irb(main):010:1&gt; attr_accessor :bar, :groo<br />
irb(main):011:1&gt; attr_reader(:gruff)<br />
irb(main):012:1&gt; end<br />
=&gt; nil<br />
irb(main):013:0&gt; f=Foo.new<br />
=&gt; #<foo:0x2a98ad0bc0><br />
irb(main):014:0&gt; f.gruff<br />
=&gt; nil<br />
irb(main):015:0&gt; f.bar<br />
=&gt; nil</p>
<p></foo:0x2a98ad0bc0></code></p>
<p>Notice that Ruby also doesn&#8217;t care if you use parenthesis or not for the arguments; in this case it doesn&#8217;t matter whether or not the parenthesis are there, because the meaning is clear, at least to the compiler.  And now, for something completely different, an instance where meaning is not clear to the compiler.</p>
<p><code lang="text">irb(main):016:0&gt; class Foo<br />
irb(main):017:1&gt; def fud(thing)<br />
irb(main):018:2&gt; thing.reverse<br />
irb(main):019:2&gt; end<br />
irb(main):020:1&gt; end<br />
=&gt; nil<br />
irb(main):021:0&gt; f.fud "hi"<br />
=&gt; "ih"<br />
irb(main):022:0&gt; f.fud "hi" ? true : false<br />
(irb):22: warning: string literal in condition<br />
NoMethodError: undefined method `reverse' for true:TrueClass<br />
        from (irb):18:in `fud'<br />
        from (irb):22<br />
        from /usr2/jest/tools//lib/ruby/site_ruby/1.8/rubygems/dependency.rb:19<br />
irb(main):023:0&gt; f.fud("hi") ? true : false<br />
=&gt; true<br />
</code></p>
<p>In the first instance, it is trying to evaluate<br />
<code lang="ruby">"hi" ? true : false</code><br />
and then pass it to f.fud.  This doesn&#8217;t work very well, since true does not have a reverse method.  We&#8217;re running into an issue of order of operation.  However, when we do<br />
<code lang="ruby">f.fud("hi") ? true : false</code><br />
then we are successful.  &#8220;hi&#8221; can be reversed and the result can be checked for non-nil-ness.  In this (admittedly simplistic) example, the parenthesis were needed in order to achieve a satisfactory result.</p>
<p>In terms of background, I learned C 20 years ago.  I cut my teeth on basic back in 1980.  So I&#8217;ve been around for a little while.  I mention this because several posters mentioned that one&#8217;s preference for using parenthesis may depend on one&#8217;s background, with those who come from C potentially more likely to use parenthesis whether they were needed or not.</p>
<p>I am of the opinion that parenthesis can make the code harder to read.<br />
<code lang="ruby">attr_accessor :first, :last<br />
has_many :pets</code><br />
is easier to my eyes and far easier to read as it mimics &#8220;natural&#8221; language than<br />
<code lang="ruby">attr_accessor(:first, :last)<br />
has_many(:pets)</code><br />
However, I realize that it is a personal choice.  So my rule of thumb is to not use parenthesis, except where there is a question of clarity.  I think that writing clear, maintainable code is more important than adhering to a rule of always using parenthesis.  I like that Ruby, like <a class="zem_slink" title="Unix" rel="wikipedia" href="http://en.wikipedia.org/wiki/Unix">Unix</a>, generally has more way than one to do a thing.  I understand that there are people who prefer one way of doing a thing.  For those people, there&#8217;s <a class="zem_slink" title="Python (programming language)" rel="homepage" href="http://www.python.org/">Python</a>.  I&#8217;m kidding, of course &#8212; you can adhere to doing things a single way, such as always using parenthesis, in Ruby.   To me, however, it would lessen my enjoyment of the language.  And I write code in Ruby because it makes me happy to do so.</p>
<p>So, what do you think?  Is there a compelling reason to use parentheses?</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://www.zemanta.com/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/zemified_a.png?x-id=62d457e3-7200-4aaa-a4ff-19722542ab42" alt="Enhanced by Zemanta"></a></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/3qOfa4ZPZV0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/02/20/to-paren-or-not-to-paren/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/02/20/to-paren-or-not-to-paren/</feedburner:origLink></item>
		<item>
		<title>my_methods revisited</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/EmgaZhGBUjI/</link>
		<comments>http://blender.matthewkwilliams.com/2009/02/19/my_methods-revisited/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 11:00:58 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=50</guid>
		<description><![CDATA[In irbrc goodness, I touched briefly on my_methods, a method of discovering the methods of an object which are distinct from those inherited from Object. The method looks like this: class Object def my_methods (self.methods - Object.methods).sort end end I have since had the idea of extending it &#8212; now you can also choose to [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://blender.matthewkwilliams.com/2009/02/18/irbrc-goodness/">irbrc goodness</a>, I touched briefly on my_methods, a method of discovering the methods of an object which are distinct from those inherited from Object.  The method looks like this:</p>
<p><code lang="ruby">class Object<br />
  def my_methods<br />
    (self.methods - Object.methods).sort<br />
  end<br />
end</code><br />
I have since had the idea of extending it &#8212; now you can also choose to see only the methods which are implemented by an object&#8217;s Class.  You can see it in use here (code at the end):<br />
$ irb<br />
>> class Object<br />
>>     def my_methods(_super=false)<br />
>>         _methods = (_super) ? self.class.superclass.new.methods : Object.methods<br />
>>         (self.methods &#8211; _methods).sort<br />
>>       end<br />
>>   end<br />
=> nil<br />
>> class Foo < String<br />
>>   def fud<br />
>>     puts &#8220;fud&#8221;<br />
>>     end<br />
>>   end<br />
=> nil<br />
>> f=Foo.new<br />
=> &#8220;&#8221;<br />
>> f.my_methods<br />
=> ["%", "*", "+", "<<", "[]&#8220;, &#8220;[]=&#8221;, &#8220;all?&#8221;, &#8220;any?&#8221;, &#8220;between?&#8221;, &#8220;capitalize&#8221;, &#8220;capitalize!&#8221;, &#8220;casecmp&#8221;, &#8220;center&#8221;, &#8220;chomp&#8221;, &#8220;chomp!&#8221;, &#8220;chop&#8221;, &#8220;chop!&#8221;, &#8220;collect&#8221;, &#8220;concat&#8221;, &#8220;count&#8221;, &#8220;crypt&#8221;, &#8220;delete&#8221;, &#8220;delete!&#8221;, &#8220;detect&#8221;, &#8220;downcase&#8221;, &#8220;downcase!&#8221;, &#8220;dump&#8221;, &#8220;each&#8221;, &#8220;each_byte&#8221;, &#8220;each_line&#8221;, &#8220;each_with_index&#8221;, &#8220;empty?&#8221;, &#8220;entries&#8221;, &#8220;find&#8221;, &#8220;find_all&#8221;, &#8220;fud&#8221;, &#8220;grep&#8221;, &#8220;gsub&#8221;, &#8220;gsub!&#8221;, &#8220;hex&#8221;, &#8220;index&#8221;, &#8220;inject&#8221;, &#8220;insert&#8221;, &#8220;intern&#8221;, &#8220;is_binary_data?&#8221;, &#8220;is_complex_yaml?&#8221;, &#8220;length&#8221;, &#8220;ljust&#8221;, &#8220;lstrip&#8221;, &#8220;lstrip!&#8221;, &#8220;map&#8221;, &#8220;match&#8221;, &#8220;max&#8221;, &#8220;member?&#8221;, &#8220;min&#8221;, &#8220;next&#8221;, &#8220;next!&#8221;, &#8220;oct&#8221;, &#8220;partition&#8221;, &#8220;reject&#8221;, &#8220;replace&#8221;, &#8220;reverse&#8221;, &#8220;reverse!&#8221;, &#8220;rindex&#8221;, &#8220;rjust&#8221;, &#8220;rstrip&#8221;, &#8220;rstrip!&#8221;, &#8220;scan&#8221;, &#8220;select&#8221;, &#8220;size&#8221;, &#8220;slice&#8221;, &#8220;slice!&#8221;, &#8220;sort&#8221;, &#8220;sort_by&#8221;, &#8220;split&#8221;, &#8220;squeeze&#8221;, &#8220;squeeze!&#8221;, &#8220;strip&#8221;, &#8220;strip!&#8221;, &#8220;strip_tags&#8221;, &#8220;sub&#8221;, &#8220;sub!&#8221;, &#8220;succ&#8221;, &#8220;succ!&#8221;, &#8220;sum&#8221;, &#8220;swapcase&#8221;, &#8220;swapcase!&#8221;, &#8220;to_f&#8221;, &#8220;to_i&#8221;, &#8220;to_str&#8221;, &#8220;to_sym&#8221;, &#8220;tr&#8221;, &#8220;tr!&#8221;, &#8220;tr_s&#8221;, &#8220;tr_s!&#8221;, &#8220;unpack&#8221;, &#8220;upcase&#8221;, &#8220;upcase!&#8221;, &#8220;upto&#8221;, &#8220;zip&#8221;]<br />
>> f.my_methods(true)<br />
=> ["fud"]<br />
>></p>
<p>Here&#8217;s the promised code:<br />
<code lang="ruby">class Object<br />
  def my_methods(_super=false)<br />
    _methods = (_super) ? self.class.superclass.new.methods : Object.methods<br />
    (self.methods - _methods).sort<br />
  end<br />
end</code></p>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/EmgaZhGBUjI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/02/19/my_methods-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/02/19/my_methods-revisited/</feedburner:origLink></item>
		<item>
		<title>irbrc goodness</title>
		<link>http://feedproxy.google.com/~r/RubyBlender/~3/G9FPc5ORMWc/</link>
		<comments>http://blender.matthewkwilliams.com/2009/02/18/irbrc-goodness/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 22:26:13 +0000</pubDate>
		<dc:creator>Matt Williams</dc:creator>
				<category><![CDATA[configuration]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubygems]]></category>
		<category><![CDATA[irb]]></category>
		<category><![CDATA[irbc]]></category>

		<guid isPermaLink="false">http://blender.matthewkwilliams.com/?p=47</guid>
		<description><![CDATA[Here&#8217;s my (current) .irbrc, with comments # I am including gems in my irb session. Therefore, the next require require 'rubygems' # wirble is a wonderful gem which add nice features to irb require 'wirble' # Dr. Nic's useful helper gem. It makes it so you can say: # foo.map_by_bar # instead of having to [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s my (current) .irbrc, with comments</p>
<p><code lang="ruby[lines]"><br />
# I am including gems in my irb session.  Therefore, the next require<br />
require 'rubygems'</p>
<p># wirble is a wonderful gem which add nice features to irb<br />
require 'wirble'</p>
<p># Dr. Nic's useful helper gem.  It makes it so you can say:<br />
#   foo.map_by_bar<br />
# instead of having to say:<br />
#   foo.map{|f| f.bar}<br />
# more to the point you can do<br />
#   people.map_by_first_and_last<br />
# instead of<br />
#   people.map{|p| [p.first, p.last]}<br />
require 'map_by_method'</p>
<p># what_methods is a way to see which methods, when performed on an object, return a particular value:<br />
# >> "hi".what? 'h'<br />
# "hi".chop! == "h"<br />
# "hi".chop == "h"<br />
# => ["chop!", "chop"]<br />
require 'what_methods'</p>
<p># pretty print --> provides a "pretty" view of an object<br />
require 'pp'</p>
<p># auto indent irb; it's useful for when you're just experimenting<br />
IRB.conf[:AUTO_INDENT]=true</p>
<p># start wirble (with color)<br />
Wirble.init<br />
#Wirble.colorize</p>
<p># What are the methods which belong only to an object, and not those inherited from Object<br />
class Object<br />
  def my_methods<br />
    (self.methods - Object.methods).sort<br />
  end<br />
end</p>
<p># strip html tags from a string<br />
class String<br />
  def strip_tags<br />
    self.gsub(/<\S[^><]*>/,"")<br />
  end<br />
end</code></p>
<p>I&#8217;ve shown mine.  Show me yours?</p>
<p>Here are links to the referenced <a class="zem_slink" title="RubyGems" rel="homepage" href="http://docs.rubygems.org">rubygems</a>:</p>
<ul>
<li><a href="http://rubyfurnace.com/docs/what_methods-1.0.0/">what_methods</a></li>
<li><a href="http://drnicwilliams.com/category/ruby/gems/map_by_method/">map_by_method</a></li>
<li><a href="http://pablotron.org/software/wirble/">wirble</a></li>
</ul>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=6bfc1632-06a2-45af-97a9-fbae9b25aa85" alt="Enhanced by Zemanta" /></a></div>
<img src="http://feeds.feedburner.com/~r/RubyBlender/~4/G9FPc5ORMWc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blender.matthewkwilliams.com/2009/02/18/irbrc-goodness/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blender.matthewkwilliams.com/2009/02/18/irbrc-goodness/</feedburner:origLink></item>
	</channel>
</rss>

