[Tutorial] Storing Code As Objects - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Ruby and Ruby on Rails (https://www.supportforums.net/forumdisplay.php?fid=55) +---- Thread: [Tutorial] Storing Code As Objects (/showthread.php?tid=4321) |
[Tutorial] Storing Code As Objects - Wolskie - 01-13-2010 In Ruby, you get a number of different ways of storing code in a object form, but here we're going to look at the proc objects. The class proc is used to wrap up ruby blocks as an object. Proc objects still carry around the context in the way it was defined just like normal blocks do. Code: newproc = Proc.new { |input| puts "Input is #{input}" } Proc objects are also automatically created when a method is defined with a trailing & parameter is called with the block. Code: def we_take_a_block(x, &a_block) The out put of this snippet is shown here: Code: Proc You can pass proc objects to methods that are expecting a block: Code: proc2 = proc { |qwerty|, print qwerty, " AND " } There is a couple of ways of storing blocks of code into proc objects. This might come in handy some day - Wolskie RE: [Tutorial] Storing Code As Objects - Jordan L. - 01-14-2010 Great tutorial from the one and only, who made rPort, rbShell and Sector Sweep. This man here is a god amongst programming, and the Co-Admin of Pyrite Design. Welcome to the Support Forums, bro. And nais tutorial! |