Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Referncing values of local variables

Reply
Thread Tools

Referncing values of local variables

 
 
Thomas Luedeke
Guest
Posts: n/a
 
      11-17-2006
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/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      11-17-2006
> 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/.

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      11-17-2006
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 | http://www.velocityreviews.com/forums/(E-Mail Removed)
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

 
Reply With Quote
 
Thomas Luedeke
Guest
Posts: n/a
 
      11-17-2006
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Local Variables not getting values back Samant.Trupti@gmail.com C++ 5 06-16-2008 02:59 PM
Can local function access local variables in main program? Sullivan WxPyQtKinter Python 10 11-08-2007 02:51 PM
different handling of local variables and instance variables when undefined Tammo Tjarks Ruby 2 09-13-2007 06:29 PM
local variables and global variables king Perl Misc 2 06-27-2006 03:13 PM
problem referncing common assemblies from several WS projects (NO GAC!) raniz ASP .Net Web Services 1 08-03-2003 05:53 PM



Advertisments