I first had this code, but it wasn't working:
VIM = Vimrunner::RSpec.configure do |config|
config.reuse_server = true
config.start_vim do
vim = Vimrunner.start
vim
end
end
The configure
is just a method that does the settings for a Vimrunner server. The start_vim
method just depicts, what is supposed to be executed to start vim
. However that doesn't really matter.
What was actually correct was:
Vimrunner::RSpec.configure do |config|
config.reuse_server = true
config.start_vim do
VIM = Vimrunner.start
VIM
end
end
I don't quite understand this. In the first case I actually assigned a Proc to the VIM
constant, as I found out through irb
, whereas in the second case, the VIM
constant was correctly assigned.
So it seems, I assigned the VIM constant (which lies in the global namespace) through the use of these blocks, and that's where my understanding fails me:
How I can I assign a variable in a block in a block and let that assignment be hurled back to global namespace?
How does that work? For example I tried this code:
def foo
yield
end
foo do
a = 'Hello World!'
a
end
puts a
Which will show me an error. How can I have variables inside ruby blocks be put into the caller's scope?