Ruby closures borderline macro syntax

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

A good reminder of how much block syntax gives you:

    if user.save
      # publish event 'user.created', with payload {user: user}
      Publisher.broadcast_event('user.created', user: user)
    end
    
    def create_user(params)
      user = User.new(params)
      
      # publish event 'user.created', with payload {user: user}, using block syntax 
      # now the event will have additional data about duration and exceptions
      Publisher.broadcast_event('user.created', user: user) do 
        User.save!
        # do some more important stuff here
      end
    end

Post external references

  1. 1
    http://alma-connect.github.io/techblog/2014/03/rails-pub-sub.html
Source