Today I Learned: Ruby Binding Class
Wednesday July 25, 2018 at 11:23 pm CST
originally published 10/16/2017 on the Hashrocket TIL blog
Ruby’s Binding
class allows you to access classes, variables, and methods outside of your current scope.
class Foo
def bar
my_var = 20
binding()
end
end
Normally, if you made reference to my_var
outside of that method, you’d get an error. Binding
objects allow you to do this:
my_foo = Foo.new
foo_bar = my_foo.bar
puts foo_bar.eval('my_var')
# 20
Photo by Jonny Caspari on Unsplash