Skip to content

Instantly share code, notes, and snippets.

@drusepth
Last active December 31, 2020 02:55
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 drusepth/689b6227ae3ee563398ea0dddc3ad8f9 to your computer and use it in GitHub Desktop.
Save drusepth/689b6227ae3ee563398ea0dddc3ad8f9 to your computer and use it in GitHub Desktop.
# I have a proc that I would like to initialize a variable in scope for before it runs.
[20] pry(#<AnalyzeDocumentJob>)> [callback.class, callback.binding.class]
=> [Proc, Binding]
[21] pry(#<AnalyzeDocumentJob>)> [callback.send(:local_variables).include?(:word_count),
callback.binding.local_variables.include?(:word_count)]
=> [false, false]
# (Failed) attempts to set the variable are below
[11] pry(#<AnalyzeDocumentJob>)> callback.binding.eval("word_count = 0")
=> 0
[21] pry(#<AnalyzeDocumentJob>)> [callback.send(:local_variables).include?(:word_count),
callback.binding.local_variables.include?(:word_count)]
=> [false, false]
[13] pry(#<AnalyzeDocumentJob>)> eval("word_count = 0", callback.binding)
=> 0
[21] pry(#<AnalyzeDocumentJob>)> [callback.send(:local_variables).include?(:word_count),
callback.binding.local_variables.include?(:word_count)]
=> [false, false]
[3] pry(#<AnalyzeDocumentJob>)> callback.binding.local_variable_set(:word_count, 0)
=> 0
[4] pry(#<AnalyzeDocumentJob>)> callback.binding.local_variable_get(:word_count)
NameError: local variable `word_count' is not defined for #<Binding:0x00007fb918075868>
[12] pry(#<AnalyzeDocumentJob>)> callback.binding.instance_variables
=> []
[3] pry(#<AnalyzeDocumentJob>)> callback.instance_eval do
[3] pry(#<AnalyzeDocumentJob>)* word_count = 0
[3] pry(#<AnalyzeDocumentJob>)* call
[3] pry(#<AnalyzeDocumentJob>)* end
NoMethodError: undefined method `+' for nil:NilClass
from counting_service.rb:9:in `block in <class:CountingService>'
[10] pry(#<AnalyzeDocumentJob>)> callback.send(:eval, "word_count = 0")
=> 0
[21] pry(#<AnalyzeDocumentJob>)> [callback.send(:local_variables).include?(:word_count),
callback.binding.local_variables.include?(:word_count)]
=> [false, false]
[41] pry(#<AnalyzeDocumentJob>)> callback.binding.instance_eval do
[41] pry(#<AnalyzeDocumentJob>)* word_count = 0
[41] pry(#<AnalyzeDocumentJob>)* end
=> 0
[42] pry(#<AnalyzeDocumentJob>)> callback.binding.instance_variables
=> []
[43] pry(#<AnalyzeDocumentJob>)> callback.binding.local_variables
=> []
[36] pry(#<AnalyzeDocumentJob>)> callback.instance_eval do
[36] pry(#<AnalyzeDocumentJob>)* word_count = 0
[36] pry(#<AnalyzeDocumentJob>)* end
=> 0
[44] pry(#<AnalyzeDocumentJob>)> [callback.send(:local_variables).include?(:word_count),
callback.binding.local_variables.include?(:word_count)]
=> [false, false]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment