Defining an array of arrays as constants

I am trying to define an array of arrays as a constant in one of my classes, the code is as follows:

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

When I load the class in irb, I get:

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

I tried using% w, and everything that was done turned each into a string, so I got "[1,2,3,4]" instead of [1,2,3,4]

How to define an array of arrays as a constant?

Im using ruby ​​1.8.7.

When I define a constant in IRB, that’s fine, but when I load a class with it, 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

This file is as follows:

class TBBC
    Tags =  [[/\[b\](.*?)\[\/b\]/,'<strong>\1</strong>',@config[:strong_enabled]],
    ...
    [/\[th\](.*?)\[\/th\]/,'<th>\1</th>',@config[:table_enabled]]]
+3
source share
2 answers

The code you showed works fine. You are definitely not getting an error message for this particular line. The error is called elsewhere.

, %w . [], .

:

@config nil , , , @config[:strong_enabled].

, , - @foo , - (- , ? , ).

+6

TitleCase . , :

$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9.7.0]
$ irb --version
irb 0.9.5(05/04/13)
$ irb
irb(main):001:0> Constant = [[1,2,3,4],[5,6,7,8]]
=> [[1, 2, 3, 4], [5, 6, 7, 8]]

Ruby 1.9.1. ?

+1

Source: https://habr.com/ru/post/1753062/


All Articles