Block variable & Local variable

Discussion in 'Ruby' started by Amir Ebrahimifard, Aug 3, 2010.

  1. Hi
    What does happen for "num" variable in this code :

    array = [1,2,3,4,5]
    x = 1
    num = 1
    array.each { |num| puts num*20 + x }

    ( after this code when I type "puts num , ruby return 5 ! why? )
    --
    Posted via http://www.ruby-forum.com/.
    Amir Ebrahimifard, Aug 3, 2010
    #1
    1. Advertising

  2. Amir Ebrahimifard

    Eugen Ciur Guest

    |num| inside block iterates through array's items.
    Last item in array is 5, thus |num| value will be the one from last
    iteration of 'each' method.
    It is better to rename |num| to something else, |item| for example.
    -----
    Eugen

    On 08/03/2010 11:19 AM, Amir Ebrahimifard wrote:
    > Hi
    > What does happen for "num" variable in this code :
    >
    > array = [1,2,3,4,5]
    > x = 1
    > num = 1
    > array.each { |num| puts num*20 + x }
    >
    > ( after this code when I type "puts num , ruby return 5 ! why? )
    >
    Eugen Ciur, Aug 3, 2010
    #2
    1. Advertising

  3. Eugen Ciur wrote:
    > |num| inside block iterates through array's items.
    > Last item in array is 5, thus |num| value will be the one from last
    > iteration of 'each' method.
    > It is better to rename |num| to something else, |item| for example.
    > -----
    > Eugen


    I've tried it in my machine and the output is 1...

    C:\Users\Guglielmo>ruby -v
    ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]

    C:\Users\Guglielmo>ruby
    array = [1,2,3,4,5]
    x = 1
    num = 1
    array.each { |num| puts num*20 + x }
    puts num
    ^D
    21
    41
    61
    81
    101
    1
    --
    Posted via http://www.ruby-forum.com/.
    Guglielmo Fachini, Aug 3, 2010
    #3
  4. Amir Ebrahimifard

    Eugen Ciur Guest

    Look here
    http://svn.ruby-lang.org/repos/ruby/tags/v1_9_1_0/NEWS

    Since v 1.9.1 block arguments (in our case |num|) are always local,
    i.e in 'each' block |num| will not conflict
    with outer 'num' variable. In your machine you have 1.9.1 version.

    On 08/03/2010 11:53 AM, Guglielmo Fachini wrote:
    > Eugen Ciur wrote:
    >
    >> |num| inside block iterates through array's items.
    >> Last item in array is 5, thus |num| value will be the one from last
    >> iteration of 'each' method.
    >> It is better to rename |num| to something else, |item| for example.
    >> -----
    >> Eugen
    >>

    > I've tried it in my machine and the output is 1...
    >
    > C:\Users\Guglielmo>ruby -v
    > ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]
    >
    > C:\Users\Guglielmo>ruby
    > array = [1,2,3,4,5]
    > x = 1
    > num = 1
    > array.each { |num| puts num*20 + x }
    > puts num
    > ^D
    > 21
    > 41
    > 61
    > 81
    > 101
    > 1
    >


    -----
    Eugen
    Eugen Ciur, Aug 3, 2010
    #4
  5. Eugen Ciur wrote:
    > Look here
    > http://svn.ruby-lang.org/repos/ruby/tags/v1_9_1_0/NEWS
    >
    > Since v 1.9.1 block arguments (in our case |num|) are always local,
    > i.e in 'each' block |num| will not conflict
    > with outer 'num' variable. In your machine you have 1.9.1 version.


    It means in ruby v 1.8.6 block variable and local variable are the same
    , and not difference between them ?
    --
    Posted via http://www.ruby-forum.com/.
    Amir Ebrahimifard, Aug 3, 2010
    #5
  6. Amir Ebrahimifard

    Quintus Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Am 03.08.2010 11:02, schrieb Eugen Ciur:
    > Look here
    > http://svn.ruby-lang.org/repos/ruby/tags/v1_9_1_0/NEWS
    >
    > Since v 1.9.1 block arguments (in our case |num|) are always local,
    > i.e in 'each' block |num| will not conflict
    > with outer 'num' variable. In your machine you have 1.9.1 version.
    >


    Additionally, if you run ruby with warnings enabled, you get notified:

    marvin@ikarus:~$ ruby -w
    array = [1,2,3,4,5]
    x = 1
    num = 1
    array.each{|num| puts num*20 + x}
    - -:4: warning: shadowing outer local variable - num
    puts num
    21
    41
    61
    81
    101
    1
    marvin@ikarus:~$ ruby -v
    ruby 1.9.1p429 (2010-07-02 revision 28523) [x86_64-linux]
    marvin@ikarus:~$

    Marvin
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.10 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iEYEARECAAYFAkxX5XwACgkQDYShvwAbcNksbQCfaq3nuXGd4OZNB3c1RGYeA2xS
    2o0AoIfKBAfM8UttAfamaFxW9jwwJkOr
    =orQI
    -----END PGP SIGNATURE-----
    Quintus, Aug 3, 2010
    #6
  7. Hi --

    On Tue, 3 Aug 2010, Eugen Ciur wrote:

    > Not exactly. Blocks defines a new variable scope, however blocks have access
    > to variables defined outside block scope.
    > Thus, a variable defined outside block will be modified inside block (for
    > ruby 1.8.6).


    In Ruby 1.9.1 too, for non-parameter variables -- for example:

    a = nil
    10.times {|i| a = i }
    p a # 9


    David

    --
    David A. Black, Senior Developer, Cyrus Innovation Inc.

    The Ruby training with Black/Brown/McAnally
    Compleat Philadelphia, PA, October 1-2, 2010
    Rubyist http://www.compleatrubyist.com
    David A. Black, Aug 3, 2010
    #7
  8. Amir Ebrahimifard wrote:
    > Hi
    > What does happen for "num" variable in this code :
    >
    > array = [1,2,3,4,5]
    > x = 1
    > num = 1
    > array.each { |num| puts num*20 + x }
    >
    > ( after this code when I type "puts num , ruby return 5 ! why? )


    I tried your code i added this at the end

    puts(num)

    i have this :

    21
    41
    61
    81
    101
    1

    and i think its a correct response because we have two variables one
    have the scope in the block who have at the end of iteration 5
    the other variable have the value 1 witch is displayed

    sorry for my bad English :(
    --
    Posted via http://www.ruby-forum.com/.
    Barchil Barchil, Aug 10, 2010
    #8
    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. morrell
    Replies:
    1
    Views:
    933
    roy axenov
    Oct 10, 2006
  2. Shilpa

    Accessing global variable in a local block

    Shilpa, Nov 16, 2006, in forum: C Programming
    Replies:
    9
    Views:
    326
    Chris Torek
    Nov 16, 2006
  3. shilpa
    Replies:
    4
    Views:
    276
  4. rubyrus
    Replies:
    5
    Views:
    107
  5. Artem Voroztsov
    Replies:
    2
    Views:
    108
    Artem Voroztsov
    Apr 20, 2008
Loading...

Share This Page