apt
apt

Reputation: 137

Instance variable not avaliable in instance method call

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

Upvotes: 0

Views: 62

Answers (1)

zinovyev
zinovyev

Reputation: 2163

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)

Upvotes: 2

Related Questions