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

Discussion in 'Ruby' started by Talha Oktay, Mar 8, 2006.

  1. Talha Oktay

    Talha Oktay Guest

    ------=_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--
     
    Talha Oktay, Mar 8, 2006
    #1
    1. Advertising

  2. On Mar 8, 2006, at 3:01 AM, Talha Oktay 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 Gray II
     
    James Edward Gray II, Mar 8, 2006
    #2
    1. Advertising

  3. Talha Oktay

    Guest

    Re: newbie question about scope, variables, declarations of variablesand option strict (as in perl)

    Hi --

    On Wed, 8 Mar 2006, James Edward Gray II wrote:

    > On Mar 8, 2006, at 3:01 AM, Talha Oktay 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


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


    David

    --
    David A. Black ()
    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
     
    , Mar 8, 2006
    #3
  4. On Mar 8, 2006, at 8:39 AM, wrote:

    > Hi --
    >
    > On Wed, 8 Mar 2006, James Edward Gray II wrote:
    >
    >> On Mar 8, 2006, at 3:01 AM, Talha Oktay 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

    >
    > 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
     
    James Edward Gray II, Mar 8, 2006
    #4
  5. Talha Oktay

    Guest

    Re: newbie question about scope, variables, declarations of variablesand option strict (as in perl)

    Hi --

    On Thu, 9 Mar 2006, James Edward Gray II wrote:

    > On Mar 8, 2006, at 8:39 AM, wrote:
    >
    >> Hi --
    >>
    >> On Wed, 8 Mar 2006, James Edward Gray II wrote:
    >>
    >>> On Mar 8, 2006, at 3:01 AM, Talha Oktay 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

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


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


    David

    --
    David A. Black ()
    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
     
    , Mar 8, 2006
    #5
  6. On Mar 8, 2006, at 9:38 AM, 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 Gray II
     
    James Edward Gray II, Mar 8, 2006
    #6
  7. Talha Oktay

    Guest

    On Mar 8, 2006, at 10:38 AM, 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 Wright
     
    , Mar 8, 2006
    #7
  8. Talha Oktay

    Guest

    Re: newbie question about scope, variables, declarations of variablesand option strict (as in perl)

    Hi --

    On Thu, 9 Mar 2006, James Edward Gray II wrote:

    > On Mar 8, 2006, at 9:38 AM, 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


    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 ()
    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
     
    , Mar 8, 2006
    #8
  9. Talha Oktay

    Guest

    Re: newbie question about scope, variables, declarations of variablesand option strict (as in perl)

    Hi --

    On Thu, 9 Mar 2006, wrote:

    >
    > On Mar 8, 2006, at 10:38 AM, 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 ()
    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
     
    , Mar 8, 2006
    #9
    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. Showjumper

    Does Option Strict On add overhead?

    Showjumper, Jul 31, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    370
    John Saunders
    Jul 31, 2003
  2. Abhram Jose

    Option Strict....

    Abhram Jose, Feb 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    387
    Kevin Spencer
    Feb 6, 2004
  3. C. Moya

    Option Strict and aspnet 2.0

    C. Moya, Jan 18, 2007, in forum: ASP .Net
    Replies:
    13
    Views:
    672
    Juan T. Llibre
    Jan 19, 2007
  4. Rocky

    variable scope and use strict

    Rocky, Apr 27, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    169
    Tore Aursand
    Apr 27, 2004
  5. bernd
    Replies:
    2
    Views:
    689
    bernd
    Jan 26, 2012
Loading...

Share This Page