File: with.rb

package info (click to toggle)
ruby-stud 0.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 132 kB
  • sloc: ruby: 435; makefile: 2
file content (22 lines) | stat: -rw-r--r-- 557 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Stud
  module With
    # Run a block with arguments. This is sometimes useful in lieu of
    # explicitly assigning variables.
    # 
    # I find mainly that using 'with' is a clue that I can factor out
    # a given segment of code into a method or function.
    #
    # Example usage:
    #
    #   with(TCPSocket.new("google.com", 80)) do |s|
    #     s.write("GET / HTTP/1.0\r\nHost: google.com\r\n\r\n")
    #     puts s.read
    #     s.close
    #   end
    def with(*args, &block)
      block.call(*args)
    end

    extend self
  end
end