From: Jacob Fugal Date: 2005-12-20T06:19:48+09:00 Subject: Re: how to use a proc like a method Now that I answered the simple questions, I'd suggest some style changes/fixes: # ------------ 8< ------------ class A def initialize @av = 10 ### [1], [2] end def do(&mproc) ### [3] instance_eval &mproc end def rt ### [4] @av end end a = proc{ @av = 111 } # something different b = A.new b.do(&a) ### [5] b.do { @av = 111 } ### [6] b.rt # return -> 111 # ------------ 8< ------------ [1] Ruby doesn't need a semicolon. The newline is the statement separator. A semicolon can be used to separate multiple statements on the same line, however. [2] Be liberal with your whitespace. This is simply style, but I think you'll find most rubyists share this preference. [3] You can make a method accept a block parameter (such as Array#each does) by prepending the last parameter in the parameter list with an ampersand (&). [4] If you are defining a method with no parameters, it's acceptable to leave off the empty parentheses. This is also just style and may be debated. [5] You can change a Proc object into a block with the & prefix. (In fact, you can turn any object into a block with & and a properly defined to_proc method. Whether doing so is good practice is debatable.) [6] The obvious alternative to creating an explicit proc then converting that proc to a block. Jacob Fugal