# Newbie question about scope, variables, declarations of vari

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328>\
**Category:** Ruby\
**Created:** [March 8, 2006, 10:03am UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328 "2006-03-08T10:03:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 10:03am UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/1 "2006-03-08T10:03:47Z")

</div>

Hello,

I am trying to understand the syntax error I receive in a code similar  
to  
this.

1 require ‘logger’  
2  
3 log = logger.new #some other logger settings are ignored.

4 def func  
5 log.debug “a statement” # error is reported here when func is called  
below  
6 # some code  
7 end  
8  
9 #some code continues  
10 func

When func is called, an error is reported on line-5 saying that undefine  
local variable log etc. I understand that functions create scopes and  
log is  
seen as local variable which is not defined in that scope. As it is  
qualified with no scope operator, interpreter thinks that it is local  
but  
can not find definition of the log before it’s usage but also in the  
parameter list and I understand that. On the other hand, I can use log  
without qualifying it with a scope symbol anywhere in the same file if  
it is  
not in a function. I know that loops, if statements etc are built into  
the  
language and do not create scope. Code blocks inherit the locals. So it  
is  
meaningful that I can use it anywhere else. When I qualify log with $ as  
$log, it becomes global and I no longer receive error. I have tried it  
qualifying with @ etc. but the received the same error. What I am asking  
is,  
what is scope of log?. What kind of variable is it? It is the local or  
instance variable of what, Object? I know that func is private to the  
Object. But what about log? How can I access it in a function  
without  
making it global?

Is there a way to make variables local to a file as perl does with “my”.

Is there a strict option that prevents unintended variable creation  
because  
of typos. Is there a way force predeclaration of variables?

Thanks.

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 2:51pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/2 "2006-03-08T14:51:14Z")

</div>

On Mar 8, 2006, at 3:01 AM, Talha O. wrote:

> When I qualify log with $ as  
> $log, it becomes global and I no longer receive error. I have tried it  
> qualifying with @ etc. but the received the same error.

No, this works too (after you fix your Logger constructor call):

require ‘logger’

@log = Logger.new(“test.log”) #some other logger settings are ignored.

def func  
@log.debug “a statement” # error is reported here when func is  
called below

# some code

end

#some code continues  
func

> What I am asking is, what is scope of log?

It’s a local variable, scoped to the file it is defined in. It would  
not be available inside class, module, or method definitions though,  
because they are not closures.

> What kind of variable is it?

Local.

> It is the local or instance variable of what, Object?

To the file.

> But what about log? How can I access it in a function without  
> making it global?

You could pass it as a parameter, or make it an instance variable, as  
shown above.

> Is there a way to make variables local to a file as perl does with  
> “my”.

This is how it functions.

> Is there a strict option that prevents unintended variable creation  
> because  
> of typos. Is there a way force predeclaration of variables?

You must assign to a local before you can use it in Ruby. That’s why  
you got the error. 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/3 "2006-03-08T15:43:07Z")

</div>

Hi –

On Wed, 8 Mar 2006, James Edward G. II wrote:

> @log = Logger.new(“test.log”) #some other logger settings are ignored.
> 
> def func  
> @log.debug “a statement” # error is reported here when func is called below

That’s a different @log from the one you assigned to, though.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/4 "2006-03-08T16:38:27Z")

</div>

Hi –

On Thu, 9 Mar 2006, James Edward G. II wrote:

> > > > qualifying with @ etc. but the received the same error.
> 
> p @log.object\_id  
> 923180  
> 923180

Weird. I guess Logger does something bizarre in its constructor.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/5 "2006-03-08T16:47:33Z")

</div>

On Mar 8, 2006, at 9:38 AM, removed\_email\_address@domain.invalid wrote:

> Weird. I guess Logger does something bizarre in its constructor.

I guess I don’t understand what you are getting at. This is the  
normal behavior for any object:

Neo:~/Desktop$ ls  
test.rb  
Neo:~/Desktop$ cat test.rb  
@obj = Object.new  
p @obj.object\_id

def func  
p @obj.object\_id  
end

func  
Neo:~/Desktop$ ruby test.rb  
957270  
957270

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/6 "2006-03-08T16:07:55Z")

</div>

On Mar 8, 2006, at 8:39 AM, removed\_email\_address@domain.invalid wrote:

> > 
> 
> That’s a different @log from the one you assigned to, though.

I’m not sure why you say this, but you’re wrong. 😉 Here’s the proof:

Neo:~/Desktop$ ls  
log.rb  
Neo:~/Desktop$ cat log.rb  
require ‘logger’

@log = Logger.new(“test.log”) #some other logger settings are ignored.  
p @log.object\_id

def func  
@log.debug “a statement” # error is reported here when func is  
called below  
p @log.object\_id

# some code

end

#some code continues  
func  
Neo:~/Desktop$ ruby log.rb  
923180  
923180  
Neo:~/Desktop$ cat test.log

# Logfile created on Wed Mar 08 09:04:50 CST 2006 by logger.rb/1.5.2.7

D, [2006-03-08T09:04:50.196373 #5517] DEBUG – : a statement

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/7 "2006-03-08T16:59:48Z")

</div>

Hi –

On Thu, 9 Mar 2006, James Edward G. II wrote:

> @obj = Object.new  
> p @obj.object\_id
> 
> def func  
> p @obj.object\_id  
> end
> 
> func  
> Neo:~/Desktop$ ruby test.rb  
> 957270  
> 957270

For some reason I had hallucinated that the original example was  
inside a class definition body, rather than at the top level. Ignore  
me…

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/8 "2006-03-08T16:59:41Z")

</div>

On Mar 8, 2006, at 10:38 AM, removed\_email\_address@domain.invalid wrote:

> Weird. I guess Logger does something bizarre in its constructor.

At the top level ‘self’ references the same object in the file scope  
and in the method scope (really private instance methods of the top-  
level  
object). Why then is it puzzling that the instance variables are  
accessible from either place?

@a = ‘this is @a’  
puts “top: self: #{self.object\_id}”  
puts “top: @a: #{@a}”

def foo  
puts “foo: self: #{self.object\_id}”  
puts “foo: @a: #{@a}”  
end  
foo

put that code in a file and run it:

top: self: 1019060  
top: @a: this is @a  
foo: self: 1019060  
foo: @a: this is @a

Gary W.

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [March 8, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/newbie-question-about-scope-variables-declarations-of-vari/78328/9 "2006-03-08T17:12:01Z")

</div>

Hi –

On Thu, 9 Mar 2006, removed\_email\_address@domain.invalid wrote:

> On Mar 8, 2006, at 10:38 AM, removed\_email\_address@domain.invalid wrote:
> 
> > Weird. I guess Logger does something bizarre in its constructor.
> 
> At the top level ‘self’ references the same object in the file scope  
> and in the method scope (really private instance methods of the top-level  
> object). Why then is it puzzling that the instance variables are  
> accessible from either place?

See my last message – for some reason I projected a class definition  
body onto the example when I read it.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)
