celluloid / celluloid-io

UNMAINTAINED: See celluloid/celluloid#779 - Evented sockets for Celluloid actors
https://celluloid.io
MIT License
879 stars 93 forks source link

Calling instance_eval from initialize crashes. #108

Closed ioquatix closed 10 years ago

ioquatix commented 10 years ago

The following example fails. Is this expected behaviour?

require 'celluloid/io'

class Server
    include Celluloid::IO

    def initialize(&block)
        if block_given?
            self.instance_eval &block
        end
    end

    def bob
        puts "Hello Bob!"
    end
end

server = Server.new do
    bob
end
tarcieri commented 10 years ago

Can you make this issue on the Celluloid repo instead of Celluloid::IO? Showing the exception would help too ;)

tl;dr: yes it's expected behavior. Blocks run in the context of the caller, not the receiver, as this prevents thread synchronization issues with the state that blocks close over, so instance_eval can't work in this setup (unless explicitly whitelisted, which is possible, and maybe should be the default)

ioquatix commented 10 years ago

So, I assume the best way to fix this is to construct all the state in the receiver then set that in one go in the initialise method. I'll try it and report back.