PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > whats the error here?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
whats the error here?

Réponse
 
LinkBack Outils de la discussion
Vieux 11/03/2008, 22h45   #1
Adam Akhtar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut whats the error here?

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/.

  Réponse avec citation
Vieux 11/03/2008, 23h01   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: whats the error here?

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
  Réponse avec citation
Vieux 11/03/2008, 23h08   #3
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 s

Stefano


  Réponse avec citation
Vieux 11/03/2008, 23h08   #4
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: whats the error here?

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

  Réponse avec citation
Vieux 11/03/2008, 23h08   #5
Paul Mckibbin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: whats the error here?

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/.

  Réponse avec citation
Vieux 11/03/2008, 23h31   #6
Adam Akhtar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: whats the error here?

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/.

  Réponse avec citation
Vieux 11/03/2008, 23h32   #7
Adam Akhtar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: whats the error here?

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/.

  Réponse avec citation
Vieux 12/03/2008, 00h03   #8
Adam Akhtar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: whats the error here?

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/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h27.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15510 seconds with 16 queries