Created
October 23, 2015 14:50
-
-
Save OleMchls/f4af93190d0d844fa9db to your computer and use it in GitHub Desktop.
golaas.com sample ruby implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The code with some animation logic for demonstration. | |
life=->g,s{(0..s*s-1).map{|i|->n{n==3||(g[i]&&n==2)||nil}[[g[i-s-1],g[i-s],g[i-s+1],g[i-1],g[i+1],g[i+s-1],g[i+s],g[i+s+1]].compact.count]}} | |
size = 160 | |
grid = (1..size*size).map { rand(0..1)==1 ? 1 : nil } | |
require 'json' | |
require 'faraday' | |
conn = Faraday.new(:url => 'http://localhost:3000') do |faraday| | |
faraday.adapter :net_http_persistent # Faraday.default_adapter # make requests with Net::HTTP | |
end | |
while true do | |
grid = life[grid, size] | |
wire_grid = grid.map { |value| value ? 1 : 0 }.to_enum(:each_slice, size).to_a | |
conn.post do |req| | |
req.url '/06e7798a-34a3-49a0-a6b2-360026027907' | |
req.headers['Content-Type'] = 'application/json' | |
req.body = JSON.generate wire_grid | |
end | |
sleep 0.2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just FYI in ruby you can
loop {}
instead ofwhile true {}