[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

whats the error here?

Adam Akhtar

3/11/2008 9:45:00 PM

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

16 Answers

Robert Klemme

3/11/2008 10:01:00 PM

0

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

Stefano Crocco

3/11/2008 10:08:00 PM

0

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


Sebastian Hungerecker

3/11/2008 10:08:00 PM

0

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

Paul Mckibbin

3/11/2008 10:09:00 PM

0

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

Adam Akhtar

3/11/2008 10:31:00 PM

0

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

Adam Akhtar

3/11/2008 10:33:00 PM

0

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 help though. Really appreciate it.
--
Posted via http://www.ruby-....

Adam Akhtar

3/11/2008 11:03:00 PM

0

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

Sebastian Hungerecker

3/12/2008 8:23:00 AM

0

Adam Akhtar wrote:
> 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?

Yes, it will. If you don't want that, do:
temp = template.dup

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Jesús Gabriel y Galán

3/12/2008 8:41:00 AM

0

On Wed, Mar 12, 2008 at 12:03 AM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> 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?

Let's try:

irb(main):001:0> a = {:a => 1, :b => 2}
=> {:a=>1, :b=>2}
irb(main):002:0> b = a
=> {:a=>1, :b=>2}
irb(main):003:0> b[:c] = 3
=> 3
irb(main):004:0> a
=> {:c=>3, :a=>1, :b=>2}
irb(main):005:0>

BTW, I couldn't find an insert method for hash, so I used []=

The reason of the above behaviour is that when you do this:

temp = template

is that you now have two variables referencing the same object.
So any change done through any of those variables will change the same
underlying object, thus
making the new value available through the other variable.

There was an analogy about variables being post-it notes attached to
the objects.
An assignment as the above just puts a post-it note with the text
"temp" on the hash.

Jesus.

Adam Akhtar

3/12/2008 10:49:00 AM

0

Thank you both of you for you help. I was wondering how to create a
'copy' of the contents. looks like dup will work fine.

Thanks
--
Posted via http://www.ruby-....