Hacker News new | ask | show | jobs
by chris12321 601 days ago
In languages I'm most familiar with, you can't access variables defined outside of the function within the function unless the variable is a class or global variable. So for example in Python you can do:

  >>> x = "hello"
  >>> def test():
  ...     print(x)
  ...
  >>> test()
  hello
which seems very odd to me. In Ruby you get:

  irb(main):001> x = "hello"
  => "hello"
  irb(main):002\* def test
  irb(main):003\*   puts x
  irb(main):004> end
  => :test
  irb(main):005> test
  (irb):3:in `test': undefined local variable or method `x' for main:Object (NameError)
This makes much more sense to me, since x is defined outside of the scope of test, so why should test have access to it?