I\'m new to Ruby and wondering why I am getting an error in this situation using the \'mail\' gem in a simple Sinatra app:
post \"/email/send\" do
@recipient
As Julik says, Mail#delivery
executes your block using #instance_exec
, which simply changes self
while running a block (you wouldn't be able to call methods #to
and #from
inside the block otherwise).
What you really can do here is to use a fact that blocks are closures. Which means that it "remembers" all the local variables around it.
recipient = params[:email]
Mail.deliver do
to recipient # 'recipient' is a local variable, not a method, not an instance variable
...
end
Again, briefly:
self
#instance_exec
changes the self
; self
and are remembered by blocks because blocks are closures.