SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Any good resources in designing with blocks

    Hi.

    Apart from scouring the web for sample code, has anybody written a good article on nice ways to use blocks. I have a lot of experience with other aspects of OO, but not this one. There are a load of Smalltalk pattern books, most notably by Kent Beck, but I'd like to find this stuff out without learning Smalltalk .

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing I've found has been http://www.rubygarden.org/ruby?Examp...PatternsInRuby. It makes use of blocks and their brother procs in several places (especially the command pattern). As far as full articles on it, I can't say that I've found a whole lot of articles period about ruby design idioms.

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a brief article by Martin Fowler here:
    http://www.martinfowler.com/bliki/Closure.html

    I think one nice way of using blocks is to take care of operations where there is always an operation that has to be done before and after another operation, with control being handed to the block for the middle part.

    One example would be file handling - you open a file, do something with it, then close it. Instead of worrying about the open and close part, you could do something like:

    Code:
    class File
    	def self.open_then_close(file_name, mode)
    		file = File.open(file_name, mode)
    		yield file
    		file.close
    	end
    end
    
    File.open_then_close('somefile.txt', 'rw') do |file|
        # do something with the file here
    end
    Of course, thats a somewhat useless real world example as Ruby's built in IO#open method will do the above automatically if you provide it with a block.

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On ruby-talk, the author of KirbyBase (sort of a flat file database I think) talked about how in re-implmenting it in Ruby he "saw the light" of blocks when implementing the query function, and using blocks as filters for the where clause. Might be worth downloading it and poking around to see if you can learn something there.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lastcraft
    ... blocks. I have a lot of experience with other aspects of OO, but not this one...
    Hi Marcus,

    If you mean closures, I think they came from LISP and it's not an OO concept. They can be used to somewhat extent to make code injectios (used alot in AOP).
    Now you can decorate methods with before and after events.

    It also helps OOP because with closures one can hide the implementation details of a collection by implementing iterators for example.

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone with a cool example of an instance where a method accepting a block was a good solution for your problems? I think it is pretty neat for things like:

    Code:
    >> a = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
    => ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
    >> a.reject { |x| x.match /o/ }
    => ["red", "green", "blue"]
    I guess something similar in PHP would look like
    PHP Code:
    $a = array('red''orange''yellow''green''blue''indigo''violet');
    var_dump(array_filter($acreate_function('$e','return !preg_match("/o/", $e);'))); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  7. #7
    SitePoint Guru
    Join Date
    Dec 2003
    Location
    oz
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the feeling that blocks are more than just anonymous functions or list comprehension.

  8. #8
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Thanks for the tips guys. I guess the only way to get deeper into this is actually to use it a lot, look at code a lot and learn enough smalltalk to read the patterns books. After all, I read Java books to improve my PHP and C++ design.

    yours, Marcus

    p.s. I didn't know File.open took a block. There ya' go .
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  9. #9
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's another Martin Fowler bliki post from last month I just came across:

    http://martinfowler.com/bliki/Collec...ureMethod.html

  10. #10
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Closures in ruby can be used in a similar way to the "resource allocation is initialization." whats it's face in C++ where the Constructor / Destructor behaviour is used implement things like smart pointers and scope related locking. This is one of many uses for closures.

    I find it clever that by using blocks / closures, things like iterators such as "each" can be defined as method calls that make callbacks into the associated proc object that is passed to the method. I find it's way more elegant than those stupid iterator classes that C++ and PHP use.

    I'm hardly an experienced Ruby coder myself, but I've started playing about with a pet project now I have a wee bit more time at uni, and I've found blocks were also useful in place of the Prototype pattern. When porting some incomplete C++ code to ruby I found it much nicer to move away from the prototype pattern (then I don't need copy contructors, or clone methods like I did in C++), and use Procs to define the code to generate bullets from the active weapon in this little arcade style space shoot'em up I'm writing in spare moments of time.

    Personally, I don't think there's any good tutorials on the subject. I must admit, I found myself asking the same question when I started using Ruby 2 months ago. I think that as you start using the language more, you start to think "the Ruby way". I find over time I've started using closures in more and more over time. I also found that this pet project has allowed me to explore these thngs more. When you are writing web apps (espceially in Rails), you find your self sticking to the same techniques a lot and therefore you don't spend time thinking about other approaches.

    Again as bonefry say, using inject can be particularly useful for mathematical operations where you are working in collections of data.

    Certainly, a tutorial on this topic would be a great use for many people starting to use Ruby, as this does seem like a grey area for many newbies such as myself


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •