Browse files

syntax highlighting in readme

  • Loading branch information...
1 parent 3f1c437 commit b1578fa0f0975484adf4c6816e57ab47511bce5f @igrigorik igrigorik committed May 8, 2011
Showing with 31 additions and 31 deletions.
  1. +31 −31 README.md
View
62 README.md
@@ -17,43 +17,51 @@ This gem is a work in progress, so treat it as such.
# Example: Goroutine Generator
A simple multi-threaded consumer-producer, except without a thread or a mutex in sight! Note that by default Agent channels are unbuffered, meaning that the size is implicitly set to 1. Hence, in example below, the producer will generate a single value, and block until we call receive - rinse, repeat.
- c = Agent::Channel.new(name: 'incr', type: Integer)
+```ruby
+c = Agent::Channel.new(name: 'incr', type: Integer)
- go(c) do |c, i=0|
- loop { c << i+= 1 }
- end
+go(c) do |c, i=0|
+ loop { c << i+= 1 }
+end
- p c.receive # => 1
- p c.receive # => 2
+p c.receive # => 1
+p c.receive # => 2
+```
# Example: Multi-channel selector
A "select" statement chooses which of a set of possible communications will proceed. It looks similar to a "switch" statement but with the cases all referring to communication operations. Select will block until one of the channels becomes available:
- cw = Agent::Channel.new(:name => "select-write", :type => Integer, :size => 1)
- cr = Agent::Channel.new(:name => "select-read", :type => Integer, :size => 1)
+```ruby
+cw = Agent::Channel.new(:name => "select-write", :type => Integer, :size => 1)
+cr = Agent::Channel.new(:name => "select-read", :type => Integer, :size => 1)
- select do |s|
- s.case(cr, :receive) { |c| c.receive }
- s.case(cw, :send) { |c| c.send 3 }
- end
+select do |s|
+ s.case(cr, :receive) { |c| c.receive }
+ s.case(cw, :send) { |c| c.send 3 }
+end
+```
In example above, cr is currently unavailable to read from (since its empty), but cw is ready for writing. Hence, select will immediately choose the cw case and execute that code block.
- cr = Agent::Channel.new(:name => "select-read", :type => Integer, :size => 1)
+```ruby
+cr = Agent::Channel.new(:name => "select-read", :type => Integer, :size => 1)
- select do |s|
- s.case(cr, :receive) { |c| c.receive }
- s.default { puts :default }
- end
+select do |s|
+ s.case(cr, :receive) { |c| c.receive }
+ s.default { puts :default }
+end
+```
In this example, cr is unavailable for read (since its empty), but we also provide "default" case which is executed immediately if no other cases are matched. In other words, if no blocking.
- cr = Agent::Channel.new(:name => "select-read", :type => Integer, :size => 1)
+```ruby
+cr = Agent::Channel.new(:name => "select-read", :type => Integer, :size => 1)
- select do |s|
- s.case(cr, :receive) { |c| c.receive }
- s.timeout(1.0) { puts :timeout }
- end
+select do |s|
+ s.case(cr, :receive) { |c| c.receive }
+ s.timeout(1.0) { puts :timeout }
+end
+```
Once again, cr is empty, hence cannot be read from and since there is no default block, select will block until cr is readable, or until the timeout condition is met - which in the case above is set to 1 second.
@@ -76,12 +84,4 @@ To learn more about Go see following resources:
# License
-(The MIT License)
-
-Copyright (c) 2010 Ilya Grigorik
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+The MIT License - Copyright (c) 2011 Ilya Grigorik

0 comments on commit b1578fa

Please sign in to comment.