0

Given the situation below, why can't I access my @inst variable in the Prawn block?

calling: PdfGen.new('inst').build

class PdfGen
 @class = "class"

  def initialize(inst)
   @inst = inst
  end

  def build
    @inst  #=> 'inst'
    Prawn::Document.generate() do
      @inst  #=> nil
      @class #=> 'class'
    end
  end

end
1
  • 1
    Please include what you have tried, what you expect to happen, and what actually happens. As it is, if I copy and paste your code and run PdfGen.new('foo').build it raises an exception because you have not formatted your generate call properly. Commented Oct 31, 2017 at 18:44

1 Answer 1

2

It seems like Prawn::Document.generate() evaluates code block in it's own scope (e.g. via #instance_eval method), different from your object's scope.

You can prove it by printing puts class in this block. This will give you a picture of what's going on there)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi that is exactly it. ` def initialize(&block)` if block block.arity < 1 ? instance_eval(&block) : block[self] end end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.