newbie question about scope, variables, declarations of variables and option strict (as in perl)

T

Talha Oktay

------=_Part_15542_18527927.1141808481930
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello,

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

1 require 'logger'
2
3 log =3D 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 i=
s
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 i=
s
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.

------=_Part_15542_18527927.1141808481930--
 
J

James Edward Gray II

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 Gray II
 
D

dblack

Hi --

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

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


David

--
David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
J

James Edward Gray II

Hi --



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 Gray II
 
D

dblack

Hi --

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

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


David

--
David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
J

James Edward Gray II

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 Gray II
 
G

gwtmp01

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 Wright
 
D

dblack

Hi --

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

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 ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
D

dblack

Hi --

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 ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top