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

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

On 11.03.2008 22:45, Adam A. wrote:

        '7'=>[" # ", "@ @"," # ", "@ @", " # "],

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

Adam A. wrote:

Whats wrong with lcdchar = @template[chr][segment] ???

There’s certainly nothing wrong with the syntax (i.e. it’s perfectly
valid
ruby code). The only thing wrong with the semantics is that you’re
assigning
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
for
instance variables.

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

You can’t access lcdchar from outside the class. You can however access
the
value it points to from outside (because that’s also the return value)
and
you can even access @template from outside (via instance_variable_get
for
example). And no, that’s most certainly not the reason your IDE
complains.

All i want to do is
to copy the contents to the variable lcdchar.

Copy to where?

HTH,
Sebastian

Alle Tuesday 11 March 2008, Adam A. ha scritto:

        '7'=>[" # ", "@ @"," # ", "@ @", " # "],

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

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.

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 :wink: )

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

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

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?

On Wed, Mar 12, 2008 at 12:03 AM, Adam A. [email protected]
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.

2008/3/12, Adam A. [email protected]:

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.

But keep in mind aliasing effects when using #dup. The default
implementation does just a shallow copy.

irb(main):001:0> a1 = [“foo”]
=> [“foo”]
irb(main):002:0> a2 = a1.dup
=> [“foo”]
irb(main):003:0> a2.first.sub! /^f/, ‘F’
=> “Foo”
irb(main):004:0> a2
=> [“Foo”]
irb(main):005:0> a1
=> [“Foo”]
irb(main):006:0>

Kind regards

robert

So is there a way to create a copy of one of the elements in a hash
which will be totally unconnected so that this referencing never takes
place???

2008/3/12, Adam A. [email protected]:

So is there a way to create a copy of one of the elements in a hash
which will be totally unconnected so that this referencing never takes
place???

Create a deep copy. One often used idiom is:

copy = Marshal.load(Marshal.dump(x))

Cheers

robert

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

thank you robert