Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access the callback's initial environment? #28

Open
sadovnik opened this issue Mar 8, 2017 · 2 comments
Open

How to access the callback's initial environment? #28

sadovnik opened this issue Mar 8, 2017 · 2 comments

Comments

@sadovnik
Copy link

sadovnik commented Mar 8, 2017

Blocks that were provided to the EventEmmiter as callbacks are evaluated inside the Client class. It's easy to check:

ws.on :open do
  puts self
end

Will output:

#<WebSocket::Client::Simple::Client:0x007f890bc50698>

So code like follows wont work:

state = :initial

ws.on :open do
  state = :connected
end

What's the convenient way to access the initial environment where the block was created?

@sadovnik sadovnik changed the title How to access the callback's environment? How to access the callback's initial environment? Mar 8, 2017
@Kirillvs
Copy link

Kirillvs commented Jun 15, 2017

Maybe this way:

state = :initial
set_state = ->(s) { state = s }  
ws.on :open do
  set_state.call(:connected)
end

It works because lambda catchs current environment, and inside lamda block will be the old receiver.

@unasuke
Copy link
Contributor

unasuke commented Dec 30, 2021

@sadovnik This repository was moved ruby-jp/websocket-client-simple. If you still need this, make pull request or issue to ruby-jp/websocket-client-simple again, thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants