|
This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
|
Original Post: Digging Deep with Instance_eval
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
|
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded
|
|
I feel like instance_eval deserves a friendlier name. Especially when used in tandem with a block to reach down inside an object for a moment.
A great example of this is on the RubyGarden wiki under RubyStyleGuide/InjectComplexTestsIntoObjects. The challenge is to reduce the redundancy of the below.
f = open("myfile")
if f.stat.readable? and f.stat.writable? and f.stat.size? and f.stat.owned?
# work with file
end
My favorite solution involves instance_eval, but see how long and ugly it looks? Perhaps an answer would be aliasing a name like within for simple block calls to instance_eval.
f = open("myfile")
if f.stat.instance_eval{ readable? and writable? and size? and owned? }
# work with file
end
Read: Digging Deep with Instance_eval