Skip to content

Instantly share code, notes, and snippets.

@OleMchls
Created October 23, 2015 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OleMchls/f4af93190d0d844fa9db to your computer and use it in GitHub Desktop.
Save OleMchls/f4af93190d0d844fa9db to your computer and use it in GitHub Desktop.
golaas.com sample ruby implementation
# 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
@phoet
Copy link

phoet commented Jan 6, 2016

just FYI in ruby you can loop {} instead of while true {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment