From: Michael Morin Date: 2008-08-20T08:50:28+09:00 Subject: Re: passing a variable from a block back to the method James Dinkel wrote: > Michael Morin wrote: >> You can also use catch and throw. I think this method is a little >> cleaner. >> > > ah, I think I like the catch and throw method. That's pretty close to > what I was visualizing in my head. > > Just one thing though: > >> class File >> >> def self.change(filename) >> catch(:nochanges) do >> # read the file, execute a block, then write the file >> File.open(filename, 'r+') do|file| >> lines = file.readlines >> >> # return if not changing anything >> return if yield(lines) == false >> >> file.pos = 0 >> file.print lines >> file.truncate(file.pos) >> end >> end >> end >> end >> > > You left in the "return if yield(lines) == false", was that a typo? I'm > guessing it was. Oh yeah, that is a typo left over from the other example. That line should read just "yield lines". -- Michael Morin Guide to Ruby http://ruby.about.com/ Become an About.com Guide: beaguide.about.com About.com is part of the New York Times Company