Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Normally you'd expect the parameter of a block to be a new variable, not to grab a variable from another scope and use that instead. So the "expected" value is 4, since x = 4 on the first line.

You'd expect these two examples to output the same number:

  x = 1
  my_fun do |x|
    x = 5
  end
  puts x # => 5
and

  x = 1
  my_fun do |y|
    y = 5
  end
  puts x # => 1
where my_fun could be defined as:

  def my_fun 
    yield 2
  end


You're right, and that is the behavior in Ruby 1.9.

I posted the example I did so that those unfamiliar with Ruby would see that the parent was a contrived example to use upto in that context. Sure it's awkward b/c it's not the intended semantics of the upto method, hence the ambiguity.

Maybe an ideal language would not introduce that sort of ambiguity... I personally slightly perfer the 1.9 syntax.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: