Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:18:14+00:00 2026-05-27T06:18:14+00:00

In the following code, why does the data variable work properly as a class

  • 0

In the following code, why does the data variable work properly as a class variable but not an instance variable? (Note: I tried to simplify/reproduce only the necessary bits of the script to have the question make sense. If more is needed I can post the full script.)

require 'prawn'

class Test
  include Prawn

  def initialize (data, filename) #data is an array of arrays
    @@data = data #this is the variable in question, when @data the script fails
    @filename = filename
  end

  def to_pdf
    Document.generate("#{@filename}.pdf") do #included from Prawn
      @@data.each do |item|
        do some stuff to the data
      end
      make_table with the data that's been worked on
    end
  end
end

test_run = Test.new([[1, 2, 1], [1, 2, 2]], "testfile.pdf")
test.to_pdf

If @@data is instead @data the script returns undefined method 'each' for nil:NilClass (NoMethodError) Why is that? Shouldn’t an instance variable be equally accessible to the included module? (also why is it ok that the @filename variable is an instance method?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:18:14+00:00Added an answer on May 27, 2026 at 6:18 am

    From the fine manual:

    When using the implicit block form, Prawn will evaluate the block within an instance of Prawn::Document, simplifying your syntax. However, please note that you will not be able to reference variables from the enclosing scope within this block.

    Prawn::Document.generate "example.pdf" do
        # ...
    

    If you need to access your local and instance variables, use the explicit block form shown below. In this case, Prawn yields an instance of PDF::Document and the block is an ordinary closure:

    Prawn::Document.generate "example.pdf" do |pdf|
        #...
    

    Your block in to_pdf is being evaluated in the content of a Prawn::Document instance, not within the context of your object and that means that there is no @data instance variable so @data is created with a value of nil when you try to access it. You probably want to use a block with a single argument:

    Document.generate("#{@filename}.pdf") do |pdf|
      # do things to 'pdf' in here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I do this? (The following code does NOT work, but I hope
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
The following code does not want to compile. See the included error message. Code:
The following code does not run as rootNode is null when retrieved by name
The following code does not set the comment: string userName = yrtre.etre.423369a9-3e57-42da-934d-dae91f87a1e4; MembershipUser user
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
The following code does not give a warning with g++ 4.1.1 and -Wall .
The following code does not submit the input values: $('.button').live('click', function() { var date
The following code does not compile: //int a = ... int? b = (int?)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.