I’ve been working on Ruby & Rails for about three years now. The flexibility of Ruby language continues the amaze me. There’s are so many ways to customize the behaviour.
Came across instance_exec
in Ruby which allows executing code in the context of an instance of a class.
While I’m yet to use it actually in any code, but I can think of use cases when I’ve had to use instance_variable_get
or send
to access private variables and methods in tests.
One could argue that testing private methods and variables is not ideal – tests should cover only public behaviour, but exceptions exist nevertheless.
The blog post where I discovered this: How to Use instance_exec in Ruby on Rails
instance_exec in Official RDoc