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 894379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:22:34+00:00 2026-05-15T14:22:34+00:00

Im trying to define an array of arrays as a Constant in one of

  • 0

Im trying to define an array of arrays as a Constant in one of my classes, the code looks like this:

Constant =  [[1,2,3,4],
            [5,6,7,8]]

When I load up the class in irb I get:

NoMethodError: undefined method `[]' for nil:NilClass

I tried using %w and all that did was turn each one into a string so i got “[1,2,3,4]” instead of [1,2,3,4]

how do I define an array of arrays as a constant?

Im using ruby 1.8.7.

When I define the constant in IRB its fine, but when I load up the class with it in i get an error.

require 'file_with_class.rb'
    NoMethodError: undefined method `[]' for nil:NilClass
    from ./trainbbcode/tags.rb:2
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from (irb):1

That file looks like this:

class TBBC
    Tags =  [[/\[b\](.*?)\[\/b\]/,'<strong>\1</strong>',@config[:strong_enabled]],
    ...
    [/\[th\](.*?)\[\/th\]/,'<th>\1</th>',@config[:table_enabled]]]
  • 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-15T14:22:35+00:00Added an answer on May 15, 2026 at 2:22 pm

    The code you showed works just fine. You’re definitely not getting that error message for that particular line. The error is caused elsewhere.

    And yes, %w creates an array of strings. To create normal arrays use [] like you did.

    Edit now that you’ve shown the real code:

    @config is nil in the scope where you use it, so you get an exception when you do @config[:strong_enabled].

    Note that inside of a class definition but outside of any method definition @foo refers to the instance variable of the class object, not that of any particular instance (because which one would it refer to? There aren’t even any instances yet, when the constant is initialized).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to define a select tag in my view. My view looks like
When trying to define an array such this: $array = new SPLFixedArray(256); for ($i
I'm trying to define attributes from an array like so: [a, b, c].each do
I was trying to define an array (including other arrays as values) in a
I'm trying to define a new array through a where query but I can
I am trying to define a number of classes based on an abstract base
I am trying to define an abstract class that has operators to compare two
I'm trying to define variables within a loop. I'll drop the code here and
I'm trying to use an array of arrays in a Perl object and am
I am trying to compile this piece of code found below. However, I got

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.