From: Stefano Crocco Date: 2008-03-12T07:08:05+09:00 Subject: Re: whats the error here? Alle Tuesday 11 March 2008, Adam Akhtar ha scritto: > My IDE (aptana rails) seems to complain about syntax with this code > > def initialize(size=2) > @size = size > @template = {'1'=> [" # ", "@ @"," # ", "@ @", " # "], > '2'=>[" # ", "@ @"," # ", "@ @", " # "], > '3'=>[" # ", "@ @"," # ", "@ @", " # "], > '4'=>[" # ", "@ @"," # ", "@ @", " # "], > '5'=>[" # ", "@ @"," # ", "@ @", " # "], > '6'=>[" # ", "@ @"," # ", "@ @", " # "], > '7'=>[" # ", "@ @"," # ", "@ @", " # "], > '8'=>[" # ", "@ @"," # ", "@ @", " # "], > '9'=>[" # ", "@ @"," # ", "@ @", " # "], > '0'=>[" # ", "@ @"," # ", "@ @", " # "]} > @numrows = @template[0].length > > end > > def HorizontalBarPrint chr,segment > > lcdchar = @template[chr][segment] #### DOESNT LIKE THIS BIT > > end > > end > > Whats wrong with lcdchar = @template[chr][segment] ??? > > Is it because @template is a class variable and lcdchar is trying to > point to it thus allowing for the possibility of accesing the contents > of the class variable from outside of the class??? All i want to do is > to copy the contents to the variable lcdchar. > > Anyone got any ideas????? Your code contains a syntax error, in particular it has one 'end' too much. Yo u can check syntax errors in a file calling ruby with the -c option: ruby -c my_file.rb Aside from that, there are no syntax errors. @template can't be a class variable, because those should start with @@. It may be a class instance variable, that is, an instance variable of an object of class class, such as t his: class C @x = 2 #@x is a class instance variable end In this case, if HorizontalBarPrint is an instance method, then you'll get an error at runtime, since instance variables can be only accessed from the instance. So, when ruby sees that line, it'll create a new instance variable, @template, for the instance and set it to nil. Then, it'll try to call the [] method on it and will fail with a NoMethodError: NoMethodError: undefined method `[]' for nil:NilClass It may be that your editor notices this and thus warns you. I've never used it, so I can't tell. At any rate, if you need to access a class instance variable from somewhere else, you'll need to create an accessor for it, as you'd do for instance variables: class C @x = 2 class << self attr_reader :x end end puts C.x => 2 I hope this helps Stefano