Forum: Ruby newbie question about scope, variables, declarations of vari

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Talha O. (Guest)
on 2006-03-08 11:03
(Received via mailing list)
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.
James G. (Guest)
on 2006-03-08 15:51
(Received via mailing list)
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
unknown (Guest)
on 2006-03-08 16:43
(Received via mailing list)
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)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
James G. (Guest)
on 2006-03-08 17:07
(Received via mailing list)
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
unknown (Guest)
on 2006-03-08 17:38
(Received via mailing list)
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)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
James G. (Guest)
on 2006-03-08 17:47
(Received via mailing list)
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
unknown (Guest)
on 2006-03-08 17:59
(Received via mailing list)
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.
unknown (Guest)
on 2006-03-08 17:59
(Received via mailing list)
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)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
unknown (Guest)
on 2006-03-08 18:12
(Received via mailing list)
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)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
This topic is locked and can not be replied to.