Creating new scope

Discussion in 'Ruby' started by George Moschovitis, Sep 8, 2004.

  1. Hello everyone,

    I found the following ruby idiom in "The Ruby Way":

    x = 0

    1.times {
    x = 1
    puts x
    }

    puts x

    prints:
    1
    0

    essentialy this trick creates a new scope. Is there a more elegant
    way to do it?
    George Moschovitis

    ps: The Ruby Way is a very nice book!
    George Moschovitis, Sep 8, 2004
    #1
    1. Advertising

  2. George  Moschovitis

    Olivier D. Guest

    On 2004-09-08, George Moschovitis <> wrote:
    >
    > x = 0
    >
    > 1.times {
    > x = 1
    > puts x
    > }
    >
    > puts x
    >
    > prints:
    > 1
    > 0


    If I do this on my machine, it prints 1 and 1.

    What version of Ruby have you installed? I have the version 1.8.1
    and it works as advertised in the book "Programming Ruby" (chapter
    "The Ruby Language"):
    "If instead a variable of the same name is already established at the
    time the block executes, the block will inherit this variable."

    Your 1.times block inherits the x variable and modifies it (or maybe
    it's time for me to compile Ruby version 1.8.2?)

    --
    Olivier D.
    Olivier D., Sep 8, 2004
    #2
    1. Advertising

  3. George  Moschovitis

    Mark Hubbart Guest

    On Sep 8, 2004, at 2:00 AM, Olivier D. wrote:

    > On 2004-09-08, George Moschovitis <>
    > wrote:
    >>
    >> x = 0
    >>
    >> 1.times {
    >> x = 1
    >> puts x
    >> }
    >>
    >> puts x
    >>
    >> prints:
    >> 1
    >> 0

    >
    > If I do this on my machine, it prints 1 and 1.
    >
    > What version of Ruby have you installed? I have the version 1.8.1
    > and it works as advertised in the book "Programming Ruby" (chapter
    > "The Ruby Language"):
    > "If instead a variable of the same name is already established at the
    > time the block executes, the block will inherit this variable."
    >
    > Your 1.times block inherits the x variable and modifies it (or maybe
    > it's time for me to compile Ruby version 1.8.2?)


    Currently, creating the variable beforehand *ensures* that it will be
    inherited into the block. So, currently, this code snippet should print
    1 and 1. I tested it on a 1.9 snapshot ( a few months old, granted),
    and the old 1.6.7 that's installed on one of my machines, and they both
    print 1 and 1.

    IIUC, in the future, *all* variables will leak out, except those in
    argument lists. So still, that wouldn't change the behavior of this
    code snippet.

    In the future, I believe you will be able to get this effect this way:

    x = 0

    1.times do |x|
    x = 23
    puts x #=> prints "23"
    end

    puts x #=> prints "0"

    If I am wrong, someone please correct me :)

    cheers,
    Mark


    > --
    > Olivier D.
    Mark Hubbart, Sep 8, 2004
    #3
  4. Mark Hubbart wrote:

    > IIUC, in the future, *all* variables will leak out, except those in
    > argument lists. So still, that wouldn't change the behavior of this code
    > snippet.
    > In the future, I believe you will be able to get this effect this way:


    I think there's also going to be a local-method. Like this:

    x = 1
    local do |x|
    x = 23
    p x # => 23
    end
    p x # => 1

    > cheers,
    > Mark


    Regards,
    Florian Gross
    Florian Gross, Sep 8, 2004
    #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. Paul Opal
    Replies:
    12
    Views:
    940
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    661
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    472
  4. Xah Lee
    Replies:
    0
    Views:
    2,233
    Xah Lee
    Feb 26, 2009
  5. Replies:
    0
    Views:
    163
Loading...

Share This Page