Lexical vs Dynamic Scope

Discussion in 'Ruby' started by Tim Morgan, Jan 22, 2011.

  1. Tim Morgan

    Tim Morgan Guest

    Forgive this very basic question, but Googling has not answered my
    question, and I'm sure it's a simple one for the gurus here.

    I've been using Ruby for years, and I've always had questions about
    how it handles scope. Usually Ruby just does what I would expect it
    to.

    In reading about lexical vs. dynamic scope on various places on the
    Web, I read that Ruby has lexical (static) scope. But I cannot prove
    it to myself with code. For example, the following produces one (1) --
    not zero (0) as I would expect it to if Ruby was truly statically
    scoped:

    x = 0
    f = Proc.new { x }
    g = Proc.new {
    x = 1
    f.call
    }
    puts g.call
    # => 1

    (I purposely used Procs instead of regular methods here since Ruby
    methods cannot see the top-level "x" variable at all, which is a whole
    other issue.)

    Is Ruby really dynamically scoped?
     
    Tim Morgan, Jan 22, 2011
    #1
    1. Advertising

  2. Tim Morgan

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Sat, Jan 22, 2011 at 12:20 AM, Tim Morgan <> wrote:

    > Forgive this very basic question, but Googling has not answered my
    > question, and I'm sure it's a simple one for the gurus here.
    >
    > I've been using Ruby for years, and I've always had questions about
    > how it handles scope. Usually Ruby just does what I would expect it
    > to.
    >
    > In reading about lexical vs. dynamic scope on various places on the
    > Web, I read that Ruby has lexical (static) scope. But I cannot prove
    > it to myself with code. For example, the following produces one (1) --
    > not zero (0) as I would expect it to if Ruby was truly statically
    > scoped:
    >
    > x = 0
    > f = Proc.new { x }
    > g = Proc.new {
    > x = 1
    > f.call
    > }
    > puts g.call
    > # => 1
    >
    > (I purposely used Procs instead of regular methods here since Ruby
    > methods cannot see the top-level "x" variable at all, which is a whole
    > other issue.)
    >
    > Is Ruby really dynamically scoped?
    >
    >

    The x in g is the same as the x outside of g. There is only one single x in
    your program, so this doesn't show much. Maybe this instead?

    def show_error(f)
    f.call
    rescue => e
    e
    end

    f = lambda { x }
    show_error f # => #<NameError: undefined local variable or method `x' for
    main:Object>

    x = 1
    show_error f # => #<NameError: undefined local variable or method `x' for
    main:Object>

    g = lambda { x }
    show_error g # => 1
     
    Josh Cheek, Jan 22, 2011
    #2
    1. Advertising

  3. Tim Morgan

    Tim Morgan Guest

    On Jan 22, 1:12 am, Josh Cheek <> wrote:
    > The x in g is the same as the x outside of g. There is only one single x in
    > your program, so this doesn't show much. Maybe this instead?


    Ahhh. I see. Thanks!
     
    Tim Morgan, Jan 22, 2011
    #3
  4. Tim Morgan

    Abinoam Jr. Guest

    On Sat, Jan 22, 2011 at 4:12 AM, Josh Cheek <> wrote:
    > def show_error(f)
    > =A0f.call
    > rescue =3D> e
    > =A0e
    > end
    >
    > f =3D lambda { x }
    > show_error f # =3D> #<NameError: undefined local variable or method `x' f=

    or
    > main:Object>
    >
    > x =3D 1
    > show_error f # =3D> #<NameError: undefined local variable or method `x' f=

    or
    > main:Object>
    >
    > g =3D lambda { x }
    > show_error g # =3D> 1



    After doing the above, try this.

    def x
    "Anything returned by the (Receiver).x method"
    end

    show_error f # =3D> "Anything returned by the (Receiver).x method"
    show_error g # =3D> 1

    At compile time...
    f =3D lambda { x }
    x is not defined, and is thought to be a method!

    Look...
    http://www.ruby-forum.com/topic/724184

    Abinoam Jr.
     
    Abinoam Jr., Jan 22, 2011
    #4
    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. Matt Knepley

    Lexical Scope

    Matt Knepley, Oct 30, 2003, in forum: Python
    Replies:
    3
    Views:
    437
    Paul Clinch
    Oct 30, 2003
  2. globalrev

    python: lexical or dynamic scope?

    globalrev, May 13, 2008, in forum: Python
    Replies:
    3
    Views:
    897
    Mark Wooding
    May 14, 2008
  3. Xah Lee
    Replies:
    0
    Views:
    2,276
    Xah Lee
    Feb 26, 2009
  4. PerlFAQ Server
    Replies:
    0
    Views:
    143
    PerlFAQ Server
    Jan 21, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    141
    PerlFAQ Server
    Mar 27, 2011
Loading...

Share This Page