Referncing values of local variables

Discussion in 'Ruby' started by Thomas Luedeke, Nov 17, 2006.

  1. I apologize in advance, because I know this gets asked again and again
    (and again.....) by newbies, but my searches don't show a clear answer,
    and none of my attempts work.

    This should be simple. I want to define a constant,

    a = 3

    then reference the value of a in arguments like,

    temp_array = Array.new(a)


    such that the temp_array is assigned a size of 3.


    This always seems to result in complaints about undefined local variable
    or methods. Eval doesn't seem to make it work.

    What I am doing wrong??

    --
    Posted via http://www.ruby-forum.com/.
     
    Thomas Luedeke, Nov 17, 2006
    #1
    1. Advertising

  2. > This should be simple. I want to define a constant,
    >
    > a = 3
    >


    In ruby, constant names begin with a capital letter (it is common
    practice to use fully uppercase names) :

    MY_CONSTANT=3

    or

    My_constant=3

    Variable names starting with a lowercase letter define local variables.

    --
    Posted via http://www.ruby-forum.com/.
     
    Stefano Crocco, Nov 17, 2006
    #2
    1. Advertising

  3. Thomas Luedeke

    Guest

    Hi --

    On Sat, 18 Nov 2006, Thomas Luedeke wrote:

    > I apologize in advance, because I know this gets asked again and again
    > (and again.....) by newbies, but my searches don't show a clear answer,
    > and none of my attempts work.
    >
    > This should be simple. I want to define a constant,
    >
    > a = 3


    That's not a constant; it's a local variable. It sounds like you
    might be running into scoping issues.

    > then reference the value of a in arguments like,
    >
    > temp_array = Array.new(a)
    >
    >
    > such that the temp_array is assigned a size of 3.
    >
    >
    > This always seems to result in complaints about undefined local variable
    > or methods. Eval doesn't seem to make it work.


    I suspect you're doing something like:

    a = 3
    def my_method
    temp_array = Array.new(a)
    end

    where a has gone out of scope by the time you use it.

    You can use a constant:

    A = 3

    though that would normally be considered overkill, and bad design,
    unless it's really a constant that needs to be defined outside of any
    method, rather than a local variable or method argument.


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
     
    , Nov 17, 2006
    #3
  4. Stefano Crocco wrote:
    >> This should be simple. I want to define a constant,
    >>
    >> a = 3
    >>

    >
    > In ruby, constant names begin with a capital letter (it is common
    > practice to use fully uppercase names) :
    >
    > MY_CONSTANT=3
    >
    > or
    >
    > My_constant=3
    >
    > Variable names starting with a lowercase letter define local variables.


    Umm, uh (crawls under desk in embarassment...). Thanks. :}

    --
    Posted via http://www.ruby-forum.com/.
     
    Thomas Luedeke, Nov 17, 2006
    #4
    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. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    684
    Antoon Pardon
    Nov 8, 2007
  2. Replies:
    5
    Views:
    399
    Pascal J. Bourguignon
    Jun 16, 2008
  3. raniz
    Replies:
    1
    Views:
    140
    Jay B. Harlow [MVP - Outlook]
    Aug 3, 2003
  4. Tammo Tjarks
    Replies:
    2
    Views:
    294
    Tammo Tjarks
    Sep 13, 2007
  5. king
    Replies:
    2
    Views:
    177
    Tad McClellan
    Jun 27, 2006
Loading...

Share This Page