Local Variable Scope in Ruby2

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Local Variable Scope in Ruby2

    Hi,
    I was going through matz's slides from the Ruby Conference 2003, and was checking out what the slides claimed was the most regrettable behaviour in ruby, namely that in the following code,

    def foo
    a = nil
    ary.each do |b|
    c = b
    a = b
    end
    end


    b and c are local to the each-block while the variable a which is referred to in the block is not. i assumed that in Ruby2, 'a' too would be local to the block and there would be different way to refer to the variable a which is outside the block's scope.

    However, when i checked out the next slide, "Horror Of Horrors!" :-) , in Ruby2, both a and c would be local to the entire method instead!
    Now i'm just learning ruby, so i guess i'm missing something or have understood something wrong.
    What i'd like to know is how is this new behaviour better than the old behaviour? Compared to Ruby2's rule, isn't the old rule more intuitive?


    Gavri Savio Fernandez
    ___________________________________________

    If only God would give me some clear sign! Like making a large deposit in my name at a Swiss bank. - Woody Allen


    Gavri Savio Fernandez Guest

  2. Similar Questions and Discussions

    1. Local Scope Variables
      Hi, I'm having a bit of trouble with using some local scope variables. I have a function where I want to define a variable, then use that...
    2. [PHP-DEV] Variable Scope
      Variable scope is mediocre at best. For instances: $array = array(1, 2, 3, 4, 5); for ($i = 0; $i < 5; $i++) { $num = $array; echo $num;...
    3. Re[2]: [PHP-DEV] Variable Scope
      Hello LingWitt, - PHP is typeless - for doesn't span any declaration level and hence does not have its own symbol table - PHP is not c, not C++...
    4. Enumerate Local Variable Scope
      Does anyone know of a way to enumerate the local variable scope? The reason I ask is that I have a function that dumps the contents of...
    5. variable scope in c#
      I'm calling this procedure from a custom validator OnServerValidate event. I keep getting the error : Use of unassigned local variable...
  3. #2

    Default Re: Local Variable Scope in Ruby2

    il Mon, 17 Nov 2003 03:08:26 +0900, "Gavri Savio Fernandez"
    <Gavri_F@infosys.com> ha scritto::
    >Hi,
    >I was going through matz's slides from the Ruby Conference 2003, and was checking out what the slides claimed was the most regrettable behaviour in ruby, namely that in the following code,
    >
    >def foo
    > a = nil
    > ary.each do |b|
    > c = b
    > a = b
    > end
    >end
    >
    >
    >b and c are local to the each-block while the variable a which is referred to in the block is not. i assumed that in Ruby2, 'a' too would be local to the block and there would be different way to refer to the variable a which is outside the block's scope.
    the point is that you don't need to explicitly use a declaration of a
    variable outside of the scope of the block.
    If you want to use block local variables you would just need to do:

    ary.each do |normal_var,block_local_var_initialized_to_nil |
    use the vars
    end

    BTW probably you need block local vars rarely (apart from those
    declared in the || )


    gabriele renzi Guest

  4. #3

    Default Re: Local Variable Scope in Ruby2

    On Mon, Nov 17, 2003 at 03:08:26AM +0900, Gavri Savio Fernandez wrote:
    > Hi,
    > I was going through matz's slides from the Ruby Conference 2003, and was checking out what the slides claimed was the most regrettable behaviour in ruby, namely that in the following code,
    >
    > def foo
    > a = nil
    > ary.each do |b|
    > c = b
    > a = b
    > end
    > end
    >
    >
    > b and c are local to the each-block while the variable a which is referred to in the block is not. i assumed that in Ruby2, 'a' too would be local to the block and there would be different way to refer to the variable a which is outside the block's scope.
    >
    > However, when i checked out the next slide, "Horror Of Horrors!" :-) , in Ruby2, both a and c would be local to the entire method instead!
    Simple rule (Ruby 2.0): Block parameters are local to the block,
    everything else is not.

    Consider the following example (Ruby 1.8):

    a = 3
    [1,2].each do |a|
    p a
    end
    p a # => 2

    This problem is fixed with 2.0 (a => 3).

    I am not sure if the ability to mark variables explicitly as block-local
    is consider by matz, e.g.:

    ary.each do |b| <c, a>
    end

    or

    ary.each do |b|
    local c, a
    end

    For me, this looks not too bad and would fix all problems (hopefully).
    This would mean that c and a are block local. By ommiting the "a"
    inside < and >, one could access the outer a variable.

    Many new things in Rite seem to be strange at the first look, and
    luckily, there's still room for many discussions and improvements.

    Regards,

    Michael

    Michael Neumann Guest

  5. #4

    Default Re: Local Variable Scope in Ruby2

    Michael Neumann wrote:
    > Simple rule (Ruby 2.0): Block parameters are local to the block,
    > everything else is not.
    >
    > Consider the following example (Ruby 1.8):
    >
    > a = 3
    > [1,2].each do |a|
    > p a
    > end
    > p a # => 2
    >
    > This problem is fixed with 2.0 (a => 3).
    So, with the new semantics, how do we do things like ...

    a = nil

    some_collection.each do |x|
    if some_condition
    a = ...
    break
    end
    end

    Obviously, if we want "a" to end up being the same as one of the values in the collection, there's probably some iterator other than "each" that will allow for that. However, if the value we want to give it is more complex (or just plain different from the current value of x), what's the right mechanism?

    Sorry, I can't think of a concrete example this second, but I can remember doing this kind of thing many times in the past ... mainly because it always failed until I added the "a = nil" line :-).

    I ask because I might as well start preparing for when the current mechanism stops working, by using the "right" ... or should that be "Rite" :-) ... code now.




    Harry Ohlsen Guest

  6. #5

    Default Re: Local Variable Scope in Ruby2

    On Mon, Nov 17, 2003 at 07:21:15AM +0900, Harry Ohlsen wrote:
    > Michael Neumann wrote:
    > >Simple rule (Ruby 2.0): Block parameters are local to the block,
    > >everything else is not.
    > >
    > >Consider the following example (Ruby 1.8):
    > >
    > > a = 3
    > > [1,2].each do |a|
    > > p a
    > > end
    > > p a # => 2
    > >
    > >This problem is fixed with 2.0 (a => 3).
    >
    > So, with the new semantics, how do we do things like ...
    >
    > a = nil
    >
    > some_collection.each do |x|
    > if some_condition
    > a = ...
    > break
    > end
    > end
    This will work exactly the same way as in Ruby 1.8. The assignment
    inside the block will assign to the outer variable (a). Any assignment
    inside a block with the exception of assignements to block parameter
    variables (here x), will be to variables outside the block.
    > Obviously, if we want "a" to end up being the same as one of the values in
    > the collection, there's probably some iterator other than "each" that will
    > allow for that. However, if the value we want to give it is more complex
    > (or just plain different from the current value of x), what's the right
    > mechanism?
    >
    > Sorry, I can't think of a concrete example this second, but I can remember
    > doing this kind of thing many times in the past ... mainly because it
    > always failed until I added the "a = nil" line :-).
    In Rite you won't need a "a = nil" line anymore. "a=" inside the block
    will automatically introduce a variable "a" outside the block.


    Hopefully, I don't tell you something wrong :-)

    Regards,

    Michael

    Michael Neumann Guest

  7. #6

    Default Re: Local Variable Scope in Ruby2

    Michael Neumann wrote:
    >>a = nil
    >>
    >>some_collection.each do |x|
    >> if some_condition
    >> a = ...
    >> break
    >> end
    >>end
    >
    >
    > This will work exactly the same way as in Ruby 1.8. The assignment
    > inside the block will assign to the outer variable (a). Any assignment
    > inside a block with the exception of assignements to block parameter
    > variables (here x), will be to variables outside the block.
    Ah! Thanks Michael. I completely misread Matz's slide.
    > In Rite you won't need a "a = nil" line anymore. "a=" inside the block
    > will automatically introduce a variable "a" outside the block.
    This is much nicer. I'm more likely to get it right first time, now :-).

    Cheers,

    Harry O.



    Harry Ohlsen Guest

  8. #7

    Default Re: Local Variable Scope in Ruby2

    gabriele renzi <surrender_it@remove.yahoo.it> wrote in message news:<vvlfrvc44aounjjlq5vtjk9mj9pllmjfdt@4ax.com>. ..
    >
    > the point is that you don't need to explicitly use a declaration of a
    > variable outside of the scope of the block.
    > If you want to use block local variables you would just need to do:
    >
    > ary.each do |normal_var,block_local_var_initialized_to_nil |
    > use the vars
    > end
    >
    does this mean that i can include whatever local variables (local to
    the block) that i need between the || right after the block
    parameters. would such variables not affect any variable of the same
    name used outside the block and not have any effect on such variables?

    for example,

    g = 5; puts g; [1, 2, 3].each do |f, g| puts f, g; end; puts g

    now gives me:

    5
    1
    nil
    2
    nil
    3
    nil
    nil

    Under Ruby2, would it give me the following?

    5
    1
    nil
    2
    nil
    3
    nil
    5

    Gavri Savio Fernandez
    ___________________________________________
    What if everything is an illusion and nothing exists? In that case, I
    definitely overpaid for my carpet. - Woody Allen
    Gavri Savio Fernandez Guest

  9. #8

    Default Re: Local Variable Scope in Ruby2

    On Monday, November 17, 2003, 9:21:15 AM, Harry wrote [snipped]:
    > So, with the new semantics, how do we do things like ...
    > a = nil
    > some_collection.each do |x|
    > if some_condition
    > a = ...
    > break
    > end
    > end

    This works in 1.8 and looks better than the above code:

    a = (1..10).each do |n|
    if n > 3
    break n
    end
    end

    # a == 4

    Gavin


    Gavin Sinclair Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139