Gray's Matter
Justice Gray - North America's favorite metrosexual software consultant
E-mail Justice
Subscribe to this feed
Subscribe to comments
Perennially Popular Posts
How to write a presentation abstract
My grandmother, globalization, and what's wrong with DateTime.Parse
A letter to my Worldvision sponsor child
Development Methodologies for the 21st Century
My grandmother & the difference between strong typing and weak typing
How I am becoming a better developer, part 1 of infinity
Facebook RSS Feeds: How to Write Your Own With C#
The Ten Keys To Happiness
My grandmother and the Global Assembly Cache (And Star Wars)
My grandmother and the difference between processes and threads
How to make a meeting request
Why you should never be an independent consultant
The DasBlog 1.9 AJAX Autosave True Story, Part 2
Other "metro" developers
Ben Scheirman
Bil Simser
D'Arcy Lussier
David Woods
Donald Belcham
Donald Jessop
George Clingerman
James Kovacs
Jason Hunt
Jason Row
Jean-Paul Boodhoo
Jeffrey Palermo
Joey deVilla
John Bristowe
Jonas Avellana
Kyle Baley
Mack Male
Martin Hinselwood
Michael McClenaghan
Mike Duncan
Mike Friesen
Mo Khan
Otyce
RoboSapient
Russell Ball
Scott Hanselman
Scott Reynolds
Steven Rockarts
Tech Embassy
Terry Thibodeau
Tom Opgenorth
Vladimir Levin
The Justice Gray Fan Club
Amazing Mrs. Loquacious
Dr. Teeth
Facedown
Janey Lynne
Maxime Lamy
Ridley Thunder
Stan Woo
Vanilla Con
Wobbly*Bits
I Wish These People Updated More Than Once a Year
Anand Narayan
Jeff Perrin
<< Why the Mac rules your ass (part 1 of 10000)
Ruby 101: with special guest star WILLIAM SHATNER >>
Ruby 101: Method rewriting
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
Comments [0]
Ruby
|
Technical
|
Tracked by:
"Ruby 101: with special guest star WILLIAM SHATNER" (Gray's Matter)
[Trackback]
Name
E-mail
Home page
Remember Me
Comment (HTML not allowed)
Enter the code shown (prevents robots):
Justice Gray
: a seething cauldron of rampaging masculinity. A fighter! A brother! Your
secret lover
! He's also a software development consultant and the Vice President of the
Edmonton .NET user group
. He also greatly enjoys speaking about himself in the 3rd person.
Gray's Matter Archives
Recent Posts
Foreshadowing is over
Foreshadowing, part II
Foreshadowing
Controversy clarifications!
The most controversial interview of ALL TIME
Finally! The *winner* of the "What Justice Gray Means to Me" contest
The mystery of D'Arcy Lussier's ASP.NET MVP is solved...
Well, now that's pretty horrifying
MVPs on MVC: "No, we don't have any idea either"
Why I am happy to not be at the MVP Summit
Donald Belcham's cry for help: MS MVPs, your attention is needed
Post Categories
BookReviews
Conferences
DasBlog
EDMUG
Goals
HanselmanQuestions
HotDeveloperCorner
ILiveToInform
Leadership
Mac
OtherGrayAreas
Personal
Productivity
Ruby
Suresh
Technical
Transformers
Vancouver
Wii
<
August 2008
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
Sign In