whats the error here?

Discussion in 'Ruby' started by Adam Akhtar, Mar 11, 2008.

  1. Adam Akhtar

    Adam Akhtar Guest

    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/.
     
    Adam Akhtar, Mar 11, 2008
    #1
    1. Advertising

  2. 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
     
    Robert Klemme, Mar 11, 2008
    #2
    1. Advertising

  3. 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
     
    Stefano Crocco, Mar 11, 2008
    #3
  4. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Mar 11, 2008
    #4
  5. 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/.
     
    Paul Mckibbin, Mar 11, 2008
    #5
  6. Adam Akhtar

    Adam Akhtar Guest

    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/.
     
    Adam Akhtar, Mar 11, 2008
    #6
  7. Adam Akhtar

    Adam Akhtar Guest

    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-forum.com/.
     
    Adam Akhtar, Mar 11, 2008
    #7
  8. Adam Akhtar

    Adam Akhtar Guest

    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/.
     
    Adam Akhtar, Mar 11, 2008
    #8
  9. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Mar 12, 2008
    #9
  10. On Wed, Mar 12, 2008 at 12:03 AM, 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?


    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.
     
    Jesús Gabriel y Galán, Mar 12, 2008
    #10
  11. Adam Akhtar

    Adam Akhtar Guest

    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-forum.com/.
     
    Adam Akhtar, Mar 12, 2008
    #11
  12. 2008/3/12, Adam Akhtar <>:
    > 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

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Mar 12, 2008
    #12
  13. Adam Akhtar

    Adam Akhtar Guest

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



    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Mar 12, 2008
    #13
  14. 2008/3/12, Adam Akhtar <>:
    > 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

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Mar 13, 2008
    #14
  15. Adam Akhtar

    Adam Akhtar Guest

    Adam Akhtar, Mar 13, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew

    Whats wrong here...

    Matthew, Dec 2, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    370
    Matthew
    Dec 2, 2005
  2. kernel.lover

    whats the significance of void * here

    kernel.lover, Mar 9, 2005, in forum: C Programming
    Replies:
    5
    Views:
    411
    Lawrence Kirby
    Mar 10, 2005
  3. Replies:
    10
    Views:
    431
    Gabriel Genellina
    Feb 8, 2008
  4. whats happening here

    , Mar 25, 2008, in forum: C Programming
    Replies:
    4
    Views:
    2,589
    Eric Sosman
    Mar 25, 2008
  5. vikramtheone

    Basics of VHDL. Whats happening here?

    vikramtheone, Jun 1, 2009, in forum: VHDL
    Replies:
    1
    Views:
    498
    jeppe
    Jun 1, 2009
Loading...

Share This Page