|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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????? -- Posted via http://www.ruby-forum.com/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 11.03.2008 22:45, Adam Akhtar wrote:
> 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 This bit of code has the alignment wrong and an "end" too much. > Whats wrong with lcdchar = @template[chr][segment] ??? Nothing. > Is it because @template is a class variable and lcdchar is trying to For all that I can see @template is an instance variable. > 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????? Please show the complete code and error message. Cheers robert |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 s Stefano |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Adam Akhtar wrote:
> Whats wrong with lcdchar =3D @template[chr][segment] ??? There's certainly nothing wrong with the syntax (i.e. it's perfectly valid= =20 ruby code). The only thing wrong with the semantics is that you're assignin= g=20 to a local var which goes out of scope right away. > Is it because @template is a class variable @template is not a class variable. class variables have two @s. One @ is fo= r=20 instance variables. > and lcdchar is trying to=20 > point to it thus allowing for the possibility of accesing the contents > of the class variable from outside of the class??? You can't access lcdchar from outside the class. You can however access the= =20 value it points to from outside (because that's also the return value) and= =20 you can even access @template from outside (via instance_variable_get for=20 example). And no, that's most certainly not the reason your IDE complains. > All i want to do is=20 > to copy the contents to the variable lcdchar. Copy to where? HTH, Sebastian =2D-=20 NP: Dornenreich - Zu Tr=C3=A4umen wecke sich, wer kann Jabber: sepp2k@jabber.org ICQ: 205544826 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Adam Akhtar wrote:
> > > Anyone got any ideas????? @template is a hash, and you are attempting to retrieve the size of a 0 indexed item as opposed to '0'. That's the first thing I noticed. Are you calling it with a string (as you should) rather than an integer? Also, I'm guessing you have a class around this, rather than just an initialize. If not, you'll generate an infinite loop trying to redefine Object#initialize and that will make things bad. Mac -- Posted via http://www.ruby-forum.com/. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Sorry for the sloppy quoting of my code. Here it is in full. I made a
correction in the initialize method of passing a string and not an integer to find the length. This is my attempt of solving the rubyquiz lcd challenge. (please dont post your solution to the challenge - im determined to do it all by myself ;-) ) class Lcdscreen def initialize(size=2) @size = size @template = {'1'=> [" # ", "@ @"," # ", "@ @", " # "], '2'=>[" # ", "@ @"," # ", "@ @", " # "], '3'=>[" # ", "@ @"," # ", "@ @", " # "], '4'=>[" # ", "@ @"," # ", "@ @", " # "], '5'=>[" # ", "@ @"," # ", "@ @", " # "], '6'=>[" # ", "@ @"," # ", "@ @", " # "], '7'=>[" # ", "@ @"," # ", "@ @", " # "], '8'=>[" # ", "@ @"," # ", "@ @", " # "], '9'=>[" # ", "@ @"," # ", "@ @", " # "], '0'=>[" # ", "@ @"," # ", "@ @", " # "]} @numrows = @template['0'].length end def LCDWrite text 0.upto(@numrows) do |y| output = "" if (y%2==0) #if even print horizontal bars text.each_byte do |x| output << HorizontalBarPrint(x, y) end else #if odd print vertical bars 0.upto(@size) do |z| #have to print multiple lines for vertical bars text.each_byte do |x| output << VerticalBarPrint(x, y) end output << "/n" #after reaching the end of text add a newline end end puts output #print out the line end end def HorizontalBarPrint chr,segment lcdchar = @template[chr][segment] #more code to come but stuck on the above line it just wont work. end end ###################3 -- Posted via http://www.ruby-forum.com/. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Just gone back to my ide and it aint underlining that line anymore
(which means its happy now!)... confused but happy its happy! Thanks for all your though. Really appreciate it. -- Posted via http://www.ruby-forum.com/. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
err on a similiar note if i have a hash like the one above and i do this
temp = template if i then modify temp say using temp.insert() will it also modify the hash? -- Posted via http://www.ruby-forum.com/. |
|
![]() |
| Outils de la discussion | |
|
|