Gray's Matter
Justice Gray - North America's favorite metrosexual software consultant

I Wish These People Updated More Than Once a Year

We conclude the Ruby 101 series with a look at one of the most powerful (and one of my personal favorite) features of Ruby: dynamic method rewriting.  In this screencast, we will take a look at how attr_reader and attr_writer actually work, by defining our *own* attr_reader and attr_writer.  After that, we'll take a look at how to write a cached accessor method. 

What's a cached accessor method?  Well, let's assume that we've got a method call in C# that looks like this:

public string ReturnOurValue()
{
    // time-consuming calculations follow
    return ourCalculatedValue;
}

Many of you would probably do something similar the following to avoid doing the time-consuming calculation all the time:

public int DoCalculation()
{
     if (this.CalculatedProperty != null)
     {
       return this.CalculatedProperty;
     }
     else
    {
       string ourValue = // various time-consuming calculations
       this.CalculatedProperty = ourValue;
       return ourValue;
    }
}

That's just fine, but what if we have several different time-consuming calculations?  Do we have to duplicate this logic every time we want to cache our accessor?  Ruby allows you to do this as easily as attr_reader - this screencast will show you how!

Although this is the final chapter of the existing screencasts, I'm aware that I've promised to put up some screencasts in the future that explain other facets of Ruby, such as:
  • setting it up
  • a look at alternative IDEs
  • TCP/IP work

Trust me, these *are* coming!  In the meantime, please feel free to download the seventh screencast!  I definitely hope you have enjoyed the series.  If there are any other facets of Ruby you'd like me to explore, please drop me an E-mail or leave a comment; I'm happy to do more of these and they can also go towards my presenting on this technology in the future.

Thanks everyone!

Thursday, November 09, 2006 #

Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):