Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.