Skip to content

Instantly share code, notes, and snippets.

@koic
Last active September 6, 2016 11:18
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 koic/ea337f39012cd9efa775 to your computer and use it in GitHub Desktop.
Save koic/ea337f39012cd9efa775 to your computer and use it in GitHub Desktop.
Example codes for Hamamatsu Ruby Kaigi 01 Lightning Talks https://www.slideshare.net/koic/reading-1st-druby
#
# artist.rb
#
class Artist
def initialize(name)
@name = name
end
attr_reader :name
end
#
# server.rb
#
require './artist'
require 'socket'
class RemoteServer
def initialize(host, front)
@server = TCPServer.open(host)
@front = front
end
def start
while true
Thread.start(@server.accept) do |socket|
begin
method_name = Marshal.load(socket)
result = @front.__send__(method_name)
socket.write(Marshal.dump(result))
ensure
socket.close if socket
end
end
end
end
end
artist = Artist.new('Michael Amott')
RemoteServer.new(8989, artist).start
#
# remote_object.rb
#
require 'socket'
class RemoteObject
def initialize(host, port)
@socket = TCPSocket.open(host, port)
end
def method_missing(method_name)
@socket.write(Marshal.dump(method_name))
result = Marshal.load(@socket)
ensure
@socket.close if @socket
result
end
end
artist = RemoteObject.new('localhost', 8989)
puts artist.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment