Variable Scope in Blocks

前端 未结 1 1168
盖世英雄少女心
盖世英雄少女心 2021-01-20 19:44

David A Black (The Well Grounded Rubyist, Chapter 6) presents the following code:

def block_local_parameter
  x = 100
  [1,2,3].each do |x|
    puts         


        
1条回答
  •  不思量自难忘°
    2021-01-20 20:19

    What you're seeing is the behavior for Ruby 1.8.x. Variable scope for blocks was introduced in 1.9, switch to 1.9.x and you will get the same results as in the book.

    0 讨论(0)
提交回复
热议问题